Merge SQL Server, MySQL, and Oracle rows

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.