| Author |
Topic |
|
chinlax
Starting Member
30 Posts |
Posted - 2011-10-05 : 00:39:28
|
| Hi All,I have table say ABCBOOkname, PudidBook1a P001Book2a P001 Book3a P002But in my task i need to display them asBookname Book1a, Book2a Book2a I am able to display Book1a, Book2a DECLARE @Ids VARCHAR(1000)SELECT top(2) @Ids=ISNULL(@IDs+',','') +CAST(bookname AS VARCHAR ) ;Print @Ids;the above query returns only first row ,but i want the second row to display as Book2aCan any one help me out.Thanks in advance |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 00:44:28
|
| how book2a repeat in second row? shouldnt it be Book3a?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 00:46:01
|
if its Book3a, you can use belowSELECT Pudid,STUFF((SELECT ',' + BookName FROM ABC WHERE Pudid=t.Pudid FOR XML PATH('')),1,1,'')FROM (SELECT DISTINCT Pudid FROM ABC)t------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
chinlax
Starting Member
30 Posts |
Posted - 2011-10-05 : 03:37:29
|
| Thanks it worked out ,But i have one more query on the similar tableThis is the given tableLibid PUBID BooknameL001 ABC Book1AL001 ABC Book2AL001 XYZ Book1XYZL001 XXX Book1XL001 XXX Book2XL001 DEF Book1DL002 DEF Book2DL002 GHI Book1GL002 ABC Book2AWe should show the output asTarget TableLibID ABC DEF GHI XXX XYZL001 Book1,Book2 NULL NULL Book1x,Book2x Book1xyzL002 Book2 Book1D, Book2D Book1G NULL NULLCan u help . |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 04:16:49
|
| sorry you output is confusing. can you format it correctly and show------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
chinlax
Starting Member
30 Posts |
Posted - 2011-10-05 : 04:46:10
|
| ok the space is not enough to produce it properly so i will write each coloumn separately .LIBIDLOO1LOO2 ABCBook1A,Book2ABook2ADEFNULLBook1D,Book2DGHINULLBook1GXXXBook1x,Book2xNULLXYZBook1XYZNULLThese are the coloumns , this is like i need to display.I could not copy the table structure, so only i wrote it like this |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 04:51:25
|
| [code]SELECT Libid,STUFF((SELECT ',' + Bookname FROM table WHERE PUBID='ABC' AND Libid=t.Libid ORDER BY Bookname FOR XML PATH('')),1,1,'') AS ABC,STUFF((SELECT ',' + Bookname FROM table WHERE PUBID='DEF' AND Libid=t.Libid ORDER BY Bookname FOR XML PATH('')),1,1,'') AS DEF,STUFF((SELECT ',' + Bookname FROM table WHERE PUBID='GHI' AND Libid=t.Libid ORDER BY Bookname FOR XML PATH('')),1,1,'') AS GHI,STUFF((SELECT ',' + Bookname FROM table WHERE PUBID='XXX' AND Libid=t.Libid ORDER BY Bookname FOR XML PATH('')),1,1,'') AS XXX,STUFF((SELECT ',' + Bookname FROM table WHERE PUBID='XYZ' AND Libid=t.Libid ORDER BY Bookname FOR XML PATH('')),1,1,'') AS XYZFROM (SELECT DISTINCT Libid FROM table)t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
chinlax
Starting Member
30 Posts |
Posted - 2011-10-05 : 05:15:07
|
| Thanks a lot.....Its working 100% fine. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 05:40:28
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|