Content imported from:
Merge multiple rows and one column of data into one row and one column of data
Http://topic.csdn.net/u/20090714/17/5FE6A0F7-CE78-4936-BE31-21D462236059.html
Merge data in MySQL and Oracle
Http://www.blogjava.net/rain1102/archive/2009/06/24/283867.html
SQL Server
-- Method 2 in sql2005
Create Table TB (ID Int , Value Varchar ( 10 ))
Insert Into TB Values ( 1 , ' AA ' )
Insert Into TB Values ( 1 , ' Bb ' )
Insert Into TB Values ( 2 , ' Aaa ' )
Insert Into TB Values ( 2 , ' Bbb ' )
Insert Into TB Values ( 2 , ' CCC ' )
Go
Select ID, [ Values ] = Stuff (( Select ' , ' + [ Value ] From TB t Where ID = TB. ID
For XML Path ( '' )), 1 , 1 , '' )
From TB
Group By ID
/*
ID values
-------------------------------
1 aa, BB
2 AAA, BBB, CCC
(2 row (s) affected)
*/
Drop TableTB
MySQL
Select Name, group_concat (email Order By Email separator ",") As Email From Student Group By Name
Oracle
If the above results are to be displayed in Oracle, it is more complicated. Because there is no row merging function in Oracle, you need to use sys_connect_by_path () for implementation,CodeAs follows:
Select Name, Ltrim (Sys_connect_by_path (email, ' , ' ), ' , ' ) Email From (
Select Name, email,
Row_number () Over (Partition By Name Order By Email) Rn,
Count ( * ) Over (Partition By Name) CNT
From Student
) Where Level = CNT
Start With Rn = 1
Connect By Prior name = Name And Prior Rn + 1 = Rn