|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, April 25, 2013 7:04 AM
Points: 269,
Visits: 1,110
|
|
HI all
I have a master table with item_no as one of column
Ex: table name:Table1 ITem_no 1 2 3
Table name: Table 2 Item_no , format 1 Soap 1 pen 1 pencil 1 eraser 2 soft toys 2 adult toys 2 kids items
Now i want to merge the table with below format ,with single row as output
1 soap pen pencil eraser 2 soft toys adult toys kids items
or 1 soap, pen, pencil, eraser 2 soft toys,adult toys, kids items
Thnaks in advance
Relationship table
Many Thanks! S.saravanan “I am a slow walker, but I never walk backwards- Abraham Lincoln”
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, April 19, 2013 6:50 AM
Points: 135,
Visits: 28,511
|
|
create table #t1 (ITem_no int) insert into #t1 (Item_no) values (1) insert into #t1 (Item_no) values (2) insert into #t1 (Item_no) values (3)
create table #t2 (ITem_no int, format varchar(16)) insert into #t2 (Item_no, format) values (1, 'Soap') insert into #t2 (Item_no, format) values (1, 'Pen') insert into #t2 (Item_no, format) values (1, 'Pencil') insert into #t2 (Item_no, format) values (1, 'Eraser') insert into #t2 (Item_no, format) values (2, 'Soft Toys') insert into #t2 (Item_no, format) values (2, 'Soft Toys') insert into #t2 (Item_no, format) values (2, 'Adult Toys') insert into #t2 (Item_no, format) values (2, 'Kids Items')
select t1.*, ( select t2.format + ', ' from #t2 t2 where t2.Item_no = t1.Item_no order by t2.format for xml path('') ) from #t1 t1
http://www.dwexplorer.com @dtwegener
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, April 25, 2013 7:04 AM
Points: 269,
Visits: 1,110
|
|
Many thanks,
Many Thanks! S.saravanan “I am a slow walker, but I never walk backwards- Abraham Lincoln”
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Today @ 9:51 AM
Points: 457,
Visits: 2,675
|
|
hi, i notice in the following link, for what i see as the same problem, they give a similar answer but wrap it with the STUFF function. I've tested this on my database and there doesn't seem to be any difference in output. I'm i just not seeing something or are they the same?
http://www.sqlservercentral.com/Forums/Topic973776-392-1.aspx
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 1:20 PM
Points: 1,235,
Visits: 5,389
|
|
The Stuff() formula is used to get rid of the extra comma.
Drew
J. Drew Allen Business Intelligence Analyst Philadelphia, PA
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, January 14, 2013 4:09 AM
Points: 34,
Visits: 55
|
|
Hi Dirk Wegener, It is nice solution....
Many -Many Thanks!!!
|
|
|
|