Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Combine Multiple rows into single row

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2009-12-07 : 06:32:59
hi

I have 2 tables namely Order and Order Detail

Order
OrderID CustID
0001     1
0002     2

Order Detail
OrderID CustID ProductName Remark
0001     1     A     Good
0001     1     B     Bad
0001     1     C     Ok
0002     2     A     Good
0002     2     Z     Ok
0002     2     D     OK


How do i achieve this. The 3rd column is a concatenate of 3 orders into a OD field. Thanks
OrderID CustID OD
0001     1     A-Good,B-Bad,C-OK
0002     2     A-Good,Z-OK,D-OK

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-12-07 : 07:18:46
concatenate records without UDF


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2009-12-07 : 07:36:08
hi

Sorry, perhaps i should change the table structure:

I have 2 tables namely Order and Order Detail

Order
OrderID CustID CustName
0001 1 John
0002 2 Kent

Order Detail
OrderID CustID ProductName Remark
0001 1 A Good
0001 1 B Bad
0001 1 C Ok
0002 2 A Good
0002 2 Z Ok
0002 2 D OK


If i would to select Custname, OrderID, ProductName, Remark from Order join Order Detail, it would have a result of 6 rows.

But how do i achieve just 2 rows like
CustName OrderID OD
John,0001,A-Good,B-Bad,C-OK
kent,0002,A-Good,Z-OK,D-OK

Thanks a lot.

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-12-07 : 07:44:34
Use the method in the link i posted.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2009-12-07 : 10:15:04
Thanks
Go to Top of Page
   

- Advertisement -