Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I Have a table as below
HTML
id   suborder
==================
1    a
2    b
1    c
1    d
2    e
3    f

and i need a resultset as below
HTML
id   suborder    OtherSuborders
===============================
1     a         c,d,
2     b         e
1     c         a,d
1     d         a,c
2     e         b
3     f         null (0r) empty


Can Any one help me out?

Thanks in advance.
Posted
Comments
CHill60 1-May-13 12:18pm    
What version of SQL are your running with (as some solutions won't work on earlier versions)
HariPrasad katakam 1-May-13 12:19pm    
Sql Server 2008 R2
gvprabu 3-May-13 6:35am    
hi, suborder Column have unigue value right
HariPrasad katakam 3-May-13 6:37am    
Yes

You can concatenate rows in a table using FOR XML PATH() function. Check this out: http://sqlandme.com/2011/04/27/tsql-concatenate-rows-using-for-xml-path/
 
Share this answer
 
Comments
HariPrasad katakam 1-May-13 13:21pm    
Hi Balimusi, Thanks for your reply. I have gone through the link earlier, it is use full, but it is not giving me 100% info what exactly i am looking for.

Thanks again for your time.
Hi,
Try like as follows,
SQL
SELECT ROW_NUMBER() OVER(PARTITION BY M.id, ORDER BY M.suborder) 'RowNo',
M.id, M.suborder, STUFF((SELECT ','+suborder AS text()
FROM table_name
WHERE ROW_NUMBER() OVER(PARTITION BY id, ORDER BY suborder) <> 1
AND id=M.ID FOR XML PATH(''))),1,1,'') 'OtherSuborders'
FROM table_name M
WHERE ROW_NUMBER() OVER(PARTITION BY M.id, ORDER BY M.suborder) = 1
-- In my PC , I don't have SQL Server, So check and tel me the give Query is fine or not.

Regards,
GVPrabu
 
Share this answer
 
v2
Comments
HariPrasad katakam 3-May-13 11:38am    
Hello Prabu, Thanks for your time. The query is throwing syntactical errors.
gvprabu 3-May-13 11:43am    
yes yar, Bcos in my office machine I don't have SQL server. So check and fix that Issue or else once I reach my home. I will update the Post. :-)
gvprabu 3-May-13 11:43am    
This is the logic for your requirement...
HariPrasad katakam 3-May-13 13:32pm    
Ok thanks.will try to fix from my end with the same logic.
What you need is a theoretical concatenation aggregate. Well, the problem is, that there is no such thing in SQL Server 2008R2. But the good new is, that you can make one really easily with the CLR integration (see: http://msdn.microsoft.com/en-us/library/91e6taax(v=vs.80).aspx[^] and http://msdn.microsoft.com/en-us/library/ms182741(v=sql.105).aspx[^]).
It is a really clear and simple solution but you need two things as above: write the code (really simple), deploy and declare the aggregate in the database (for this you need administrative privileges on the server).
Look here: http://www.mssqltips.com/sqlservertip/2022/concat-aggregates-sql-server-clr-function/[^]
An other interesting reading in this topic: https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/[^]
 
Share this answer
 
Comments
HariPrasad katakam 7-May-13 12:03pm    
Thanks Zoltán Zörgő, Let me try with your solutions. I will let you know once done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900