Solved

Merge Rows in Sql Server

Posted on 2012-12-23
5
214 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 143

Accepted Solution

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

Author Comment

by:satmisha
ID: 38717003
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 143

Expert Comment

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

Author Comment

by:satmisha
ID: 38735941
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
ID: 38905049
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linked Server - SP with Param to VIew 7 25
Converting Stored Procedure to SQL Statement 5 44
Sql server Error message 3 16
What Is an Error? 2 28
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

730 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