Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi everyoneI have two tables1st tableCustomer NumberZ12564 1Z34597 2Z45679 32nd Table isItem Customera Z12564 b Z12564 b Z34597b Z45679c Z45679c Z12564I would like a result like this in MS SQL Hi everyoneI have two tables1st tableCustomer NumberZ12564 1Z34597 2Z45679 32nd Table isItem Customera 1b 1,2,3c 1,3I tried using coalesce and cast function but it did not work for me. Does anyone know how to solve such issues in MS SQL.SAR
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2010-02-11 : 15:56:29
What version of SQL server are you using?
sneupane
Starting Member
28 Posts
Posted - 2010-02-11 : 16:00:21
SQL server 2005SAROJ
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2010-02-11 : 16:06:35
Try this
SELECT DISTINCT s1.Item, STUFF((SELECT DISTINCT ',' + convert(varchar(10),s3.Number) FROM table2 AS s2 inner join table1 s3 on s2.Customer = s3.Customer WHERE s2.Item = s1.Item ORDER BY ',' + convert(varchar(10),s3.Number) FOR XML PATH('')), 1, 1, '') AS CustomersFROM table2 AS s1ORDER BY s1.Item
where table1 is your customer table and table2 is your items table