SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Concatenate query output
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Ciupaz
Posting Yak Master

Italy
232 Posts

Posted - 11/23/2012 :  08:49:37  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2210 Posts

Posted - 11/23/2012 :  08:56:55  Show Profile  Reply with Quote
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

Italy
232 Posts

Posted - 11/23/2012 :  09:17:09  Show Profile  Reply with Quote
Perfect, thank you very much Bandi. I never get this solution by myself.

Luigi
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000