Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

Group multiple rows into single row

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris McFarling - 08 Jul 2008 23:50 GMT
Take the following table...

TABLE_A

id    value
----------
10   dog
10   cat
10   moose
10   squirrel
11   dog
11   cat
11   moose
11   squirrel
12   dog
12   cat
13   dog
13   cat
13   moose
13   squirrel

I'm trying to construct a query that will return the following result:
id    value
10   dog,cat,moose,squirrel
11   dog,cat,moose,squirrel
12   dog,cat
13   dog,cat,moose,squirrel

Anyone have a quick solution?
Plamen Ratchev - 08 Jul 2008 23:59 GMT
On SQL Server 2005 you can use FOR XML PATH with empty element to
concatenate the values:

SELECT DISTINCT
          id,
          STUFF(value_list, 1, 1, '') AS list
FROM TableA AS A
CROSS APPLY (SELECT ',' + value
                   FROM TableA AS B
                   WHERE B.id = A.id
                   FOR XML PATH('')) AS T(value_list);

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Chris McFarling - 09 Jul 2008 02:05 GMT
I should have specified, I'm on SQL Server 2000.

> On SQL Server 2005 you can use FOR XML PATH with empty element to
> concatenate the values:
[quoted text clipped - 12 lines]
> Plamen Ratchev
> http://www.SQLStudio.com
Plamen Ratchev - 09 Jul 2008 02:43 GMT
Take a look at the following article, it has a few methods for SQL Server
2000:

http://www.projectdmx.com/tsql/rowconcatenate.aspx

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.