Solved

Merge Rows in Sql Server

Posted on 2012-12-23
5
206 Views
Last Modified: 2013-08-14
Hi Expert,

I have provided sample table in sql server :

ID      Name      Dep                Add
1      A               N                A-1
1      B               K                A-1
1      C               M                A-1
2      D               Z                A-2
2      E               O                A-2

Note: Above rows can be dynamic, it is not static like ID = 1 can have 3 rows as stated above or it could have more  or less than 3 as well.

Expected output is:
ID      Name1      Name2      Name3      Dep1      Dep2      Dep3      Add
1      A                  B                 C                N                K                M              A-1
2      D                  E               NULL        Z                O              NULL        A-2


Hopefully looking forward to hearing from you.
0
Comment
Question by:satmisha
  • 3
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 350 total points
Comment Utility
so, you need a "dynamic crosstab" function:
http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx
0
 

Author Comment

by:satmisha
Comment Utility
Thanks for your reply..
seems like it is not working as it is expecting columns as int whereas in above stated example it is varchar...
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
I did not test the function, the key for you is to use "othercolumns" (@OtherCols) argument ...
0
 

Author Comment

by:satmisha
Comment Utility
Thanks angelIII,

Apologies for late reply

Above stated didn't work.

But I got the solution by making sql function with special character saparated column.
0
 

Author Comment

by:satmisha
Comment Utility
request to pls close this question as provided answer was not appropiate.. n I am unable to see any option to close by my own...

Thanks
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now