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)
 Concatenate query output

Author  Topic 

Ciupaz
Posting Yak Master

232 Posts

Posted - 2012-11-23 : 08:49:37
Hello all,
I have a query that returns me these values:

Plant UP
BRINDISI BR1
BRINDISI BR3
BRINDISI BR3
BRINDISI BR4
PIACENZA PZ1
S.FILIPPO SF3
BRINDISI BRTest
BRINDISI BR-FV
PIACENZA PZ4

I have intead to get this result:

BRINDISI BR1,BR2,BR3,BR4,BRTest,BR-FV
PIACENZA PZ1,PZ4
S.FILIPPO SF3

grouped by "Plant" field e in the second field a concatenatio of UP collegated..

How can I obtain this?

Thanks in advance.

Luigi

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-23 : 08:56:55
Just replace red marked part with your table name

-- Get CSV values
SELECT t.Plant, STUFF((SELECT ',' + s.UP FROM TestTable s WHERE s.plant = t.plant FOR XML PATH('')),1,1,'') AS CSV
FROM TestTable AS t
GROUP BY t.plant
GO

--
Chandu
Go to Top of Page

Ciupaz
Posting Yak Master

232 Posts

Posted - 2012-11-23 : 09:17:09
Perfect, thank you very much Bandi. I never get this solution by myself.

Luigi
Go to Top of Page
   

- Advertisement -