Group DataTable by Column1 & Concat Column2 - linq

I have DataTable with 2 column:
Column1 Column2
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 1
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 2
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 3
41F7-F8CB-F7A1-9AC5-6C72-3A08-361F-8803 1
9714-96BD-F411-5868-4DD1-A08D-C5B1-B872 1
9714-96BD-F411-5868-4DD1-A08D-C5B1-B872 2
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 1
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 2
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 3
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 4
How can I get another DataTable by Grouping Column1 and concat Column2 values?
LINQ would be a perfect choice, but how to do this?
Result must be DataTable below:
Column1 Column2
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 1,2,3
41F7-F8CB-F7A1-9AC5-6C72-3A08-361F-8803 1
9714-96BD-F411-5868-4DD1-A08D-C5B1-B872 1,2
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 1,2,3,4

Related

how to convert column value to multiple insert rown oracle cursor

I am trying to copy value from our old db to new db where there is a change in the table structure.
Below is the structure of the table
Table1
Table1ID, WheelCount, BlindCount, OtherCount
For eg values of table 1 is like below
Table1ID, 1,2,5
Table1 is now changed to TableNew with the below
TableNewID, DisableID , Quantity.
So the value should be
TableNewID1, 1,1 here 1= WheelCount from table1
TableNewID2, 2,2 here 2= BlindCount
TableNewID3, 3,5 here 5= OtherCount
how to write a cursor to transform table1 value to the new table tableNew structure.
Table1
Table1ID WheelCount BlindCount OtherCount
1 1 2 5
2 8 10 15
A master table defined to map disableid
DisableID Type
1 wheelCount
2 blindcount
3 otherCount
Expected structure
ID **Table1ID** **DISABLEID** QUANTITY
1 1 1 1
2 1 2 2
3 1 3 5
4 2 1 8
5 2 2 10
6 2 3 15
The simplest is a UNION ALL for each column you want to turn into a row.
insert into tablenew
select table1id,1,wheelcount from table1
union all
select table1id,2,blindcount from table1
union all
select table1id,3,othercount from table1
There are other, sleeker methods for avoiding multiple passes on the first table, in case it's huge.
This is how I understood it.
Current table contents:
SQL> SELECT * FROM table1;
TABLE1ID WHEELCOUNT BLINDCOUNT OTHERCOUNT
---------- ---------- ---------- ----------
1 1 2 5
2 8 10 15
Prepare new table:
SQL> CREATE TABLE tablenew
2 (
3 id NUMBER,
4 table1id NUMBER,
5 disableid NUMBER,
6 quantity NUMBER
7 );
Table created.
Sequence (will be used to populate tablenew.id column):
SQL> CREATE SEQUENCE seq_dis;
Sequence created.
Trigger (which actually populates tablenew.id):
SQL> CREATE OR REPLACE TRIGGER trg_bi_tn
2 BEFORE INSERT
3 ON tablenew
4 FOR EACH ROW
5 BEGIN
6 :new.id := seq_dis.NEXTVAL;
7 END;
8 /
Trigger created.
Copy data:
SQL> INSERT INTO tablenew (table1id, disableid, quantity)
2 SELECT table1id, 1 disableid, wheelcount AS quantity FROM table1
3 UNION ALL
4 SELECT table1id, 2 disableid, blindcount AS quantity FROM table1
5 UNION ALL
6 SELECT table1id, 3 disableid, othercount AS quantity FROM table1;
6 rows created.
Result:
SQL> SELECT *
2 FROM tablenew
3 ORDER BY table1id, disableid;
ID TABLE1ID DISABLEID QUANTITY
---------- ---------- ---------- ----------
1 1 1 1
3 1 2 2
5 1 3 5
2 2 1 8
4 2 2 10
6 2 3 15
6 rows selected.

How to join in single row result with one row and multiple rows in another table?

I have 2 tables, tables 1 is tree structure.
Table 1:
ID NAME PARENTID
-------------------------
1 Book1 0
2 Page 1
3 Line1 2
4 Line2 2
5 Book2 0
6 Page1 5
7 Page2 5
8 Line1 6
9 Line2 6
Table 2:
ID BOOK PAGE LINE
1 1 2 4
2 5 7 9
I want to get all of rows in table 2 and show the Name of table 1 in one line
ID BOOK PAGE LINE
1 Book1 Page Line2
2 Book2 Page2 Line2
How can I show the data from multiple rows and 1 row in single row not duplicate if I use simple select. Sorry for about the stupid problem. Thanks for your help.
Use joins (tbl is second table):
select t1.id,
t2.name book,
t3.name page,
t4.name line
from tbl t1
join treeTbl t2
on t1.book = t2.id
join treeTbl t3
on t3.id=t1.page
join treeTbl t4
on t4.id=t1.line

Grouping in crystal report

Sorry for this newbie question.
I have 1 SQL Server table:
Column1 | Column2 | Column3
Row1 A 1 100
Row2 A 1 200
Row3 A 2 50
Row4 B 4 10
Row5 C 5 20
Here in this report i would like to get the output as:
Column1 | Column2 | Colum3
Row1 A 1 300
Row2 A 2 50
Row3 B 4 10
Row4 C 5 20
Thanks in advance.
Insert group on Column1
Insert group on Column2
Suppress all sections except group #2 header
Place Column1, Column2, and Sum(Column3) into the group #2 header

How to create id with AUTO_INCREMENT on a view in Oracle

can any one help to create a AUTO_INCREMENT column on a view in oracle 11g.
Thanks
While it's not possible to return a single unique identity column for a view whose underlying data does not have any single unique identifier, it is possible to return composite values that uniquely identify the data. For example given a table of CSV Data with a unique ID on each row:
create table sample (id number primary key, csv varchar2(4000));
where the CSV column contains a string of comma separated values:
insert into sample
select 1, 'a' from dual union all
select 2, 'b,c' from dual union all
select 3, 'd,"e",f' from dual union all
select 4, ',h,' from dual union all
select 5, 'j,"",l' from dual union all
select 6, 'm,,o' from dual;
The following query will unpivot the csv data and the composite values (ID, SEQ) will uniquely identify each VALue, The ID column idetifies the record the data came from, and SEQ uniquely identifies the position in the CSV:
WITH pvt(id, seq, csv, val, nxt) as (
SELECT id -- Parse out individual list items
, 1 -- separated by commas and
, csv -- optionally enclosed by quotes
, REGEXP_SUBSTR(csv,'(["]?)([^,]*)\1',1,1,null,2)
, REGEXP_INSTR(csv, ',', 1, 1)
FROM sample
UNION ALL
SELECT id
, seq+1
, csv
, REGEXP_SUBSTR(csv,'(["]?)([^,]*)\1',nxt+1,1,null,2)
, REGEXP_INSTR(csv, ',', nxt+1, 1)
FROM pvt
where nxt > 0
)
select * from pvt order by id, seq;
ID SEQ CSV VAL NXT
---------- ---------- ---------- ---------- ----------
1 1 a a 0
2 1 b,c b 2
2 2 b,c c 0
3 1 d,"e",f d 2
3 2 d,"e",f e 6
3 3 d,"e",f f 0
4 1 ,h, [NULL] 1
4 2 ,h, h 3
4 3 ,h, [NULL] 0
5 1 j,"",l j 2
5 2 j,"",l [NULL] 5
5 3 j,"",l l 0
6 1 m,,o m 2
6 2 m,,o [NULL] 3
6 3 m,,o o 0
15 rows selected.

Need oracle query to select the order by

Table contains value 1,2,3 but while display the values needs to show 2,1,3
Example
Table A
column1 column2 column3
1 Rat Animals
2 Parrot Bird
3 Lotus Flower
Need to display parrot first then Rat and Lotus which means 2,1,3
Expected Output:
column1 column2 column3
2 Parrot Bird
1 Rat Animal
3 Lotus Flower
Kindly help me out to fix the issue in order by query.
You could use CASE expression in the ORDER BY clause for the particular conditions, and let other rows retain their order.
Setup
SQL> CREATE TABLE t
2 (column1 int, column2 varchar2(6), column3 varchar2(7));
Table created.
SQL> INSERT ALL
2 INTO t (column1, column2, column3)
3 VALUES (1, 'Rat', 'Animals')
4 INTO t (column1, column2, column3)
5 VALUES (2, 'Parrot', 'Bird')
6 INTO t (column1, column2, column3)
7 VALUES (3, 'Lotus', 'Flower')
8 INTO t (column1, column2, column3)
9 VALUES (7, 'def', 'xyz')
10 INTO t (column1, column2, column3)
11 VALUES (4, 'abc', 'qwe')
12 SELECT * FROM dual;
5 rows created.
SQL> COMMIT;
Commit complete.
Table Data
SQL> SELECT * FROM t;
COLUMN1 COLUMN COLUMN3
---------- ------ -------
1 Rat Animals
2 Parrot Bird
3 Lotus Flower
7 def xyz
4 abc qwe
Required Query
SQL> SELECT * FROM t
2 ORDER BY
3 CASE column1
4 WHEN 1
5 THEN 2
6 WHEN 2
7 THEN 1
8 ELSE 3
9 END,
10 column1;
COLUMN1 COLUMN COLUMN3
---------- ------ -------
2 Parrot Bird
1 Rat Animals
3 Lotus Flower
4 abc qwe
7 def xyz
SQL>
So, you have your desired order as well as the other rows retain their order as specified.
That is a strange order requested, anyways try this -
SELECT Column1,Column2,Column3
FROM TableA
ORDER BY CASE WHEN Column1 = 2 THEN 1
WHEN Column1 = 1 THEN 2
ELSE 3
END

Resources