<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Slayer &#187; Aggregate to CSV</title>
	<atom:link href="http://www.sqlslayer.com/wp/tag/aggregate-to-csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sqlslayer.com/wp</link>
	<description>Making SQL do what we want it to do.</description>
	<lastBuildDate>Mon, 12 Dec 2011 13:53:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Aggregate to CSV</title>
		<link>http://www.sqlslayer.com/wp/2009/11/06/aggregate-to-csv/</link>
		<comments>http://www.sqlslayer.com/wp/2009/11/06/aggregate-to-csv/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 20:25:53 +0000</pubDate>
		<dc:creator>mlakarj</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Aggregate to Comma Separated List]]></category>
		<category><![CDATA[Aggregate to Comma Separated Values]]></category>
		<category><![CDATA[Aggregate to CSV]]></category>
		<category><![CDATA[Comma Separated List]]></category>
		<category><![CDATA[Comma Separated Values]]></category>
		<category><![CDATA[Concatenating Rows]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[Rollup to Comma Separated List]]></category>
		<category><![CDATA[Rollup to Comma Separated Values]]></category>
		<category><![CDATA[Rollup to CSV]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.sqlslayer.com/wp/?p=131</guid>
		<description><![CDATA[<p>Sometimes we need to rollup string values into lists of comma separated values.  Using “FOR XML PATH” per column that you want to rollup can provide a way to do this.</p>
<p>This example was created based on work in this thread:
http://www.sqlservercentral.com/Forums/Topic802508-1672-1.aspx</p>
<p>Let’s say you have a data set like so:</p>


id
fruit
city


101
Apple
Cleveland


101
Apple
Pittsburgh


101
Banana
Pittsburgh


102
Grape
Cleveland


102
Melon
Cleveland


103
Melon
Pittsburgh


103
Melon
Cleveland


<p>and for each distinct “id”, you want [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we need to rollup string values into lists of comma separated values.  Using “FOR XML PATH” per column that you want to rollup can provide a way to do this.</p>
<p>This example was created based on work in this thread:<br />
<a href="http://www.sqlservercentral.com/Forums/Topic802508-1672-1.aspx">http://www.sqlservercentral.com/Forums/Topic802508-1672-1.aspx</a></p>
<p>Let’s say you have a data set like so:</p>
<table border="2">
<tr>
<td><strong>id</strong></td>
<td><strong>fruit</strong></td>
<td><strong>city</strong></td>
</tr>
<tr>
<td>101</td>
<td>Apple</td>
<td>Cleveland</td>
</tr>
<tr>
<td>101</td>
<td>Apple</td>
<td>Pittsburgh</td>
</tr>
<tr>
<td>101</td>
<td>Banana</td>
<td>Pittsburgh</td>
</tr>
<tr>
<td>102</td>
<td>Grape</td>
<td>Cleveland</td>
</tr>
<tr>
<td>102</td>
<td>Melon</td>
<td>Cleveland</td>
</tr>
<tr>
<td>103</td>
<td>Melon</td>
<td>Pittsburgh</td>
</tr>
<tr>
<td>103</td>
<td>Melon</td>
<td>Cleveland</td>
</tr>
</table>
<p>and for each distinct “id”, you want a list of its distinct fruits and cities.  </p>
<p>If you put a FOR XML PATH statement for each column you want to rollup like so:</p>
<blockquote><p><code>...<br />
----------fruit---------<br />
,STUFF((SELECT DISTINCT ', ' + NULLIF(fruit,'')<br />
FROM tableToCSVRollup t2<br />
WHERE t1.id = t2.id<br />
FOR XML PATH(''),TYPE<br />
).value('.','VARCHAR(MAX)')<br />
,1,2,'') AS fruit<br />
...</code></p></blockquote>
<p>and then GROUP BY id, you’ll have a nice aggregated set.  It will even put it in alphabetical order and remove NULL and empty string values for you.</p>
<p>Complete code example:</p>
<blockquote><p><code>;WITH tableToCSVRollup (id,fruit,city)<br />
AS (<br />
		  SELECT 101,'Apple','Cleveland'<br />
UNION ALL SELECT 101,'Apple','Pittsburgh'<br />
UNION ALL SELECT 101,'Banana','Pittsburgh'<br />
UNION ALL SELECT 102,'Grape','Cleveland'<br />
UNION ALL SELECT 102,'Melon','Cleveland'<br />
UNION ALL SELECT 103,'Melon','Pittsburgh'<br />
UNION ALL SELECT 103,'Melon','Cleveland'<br />
)</p>
<p>SELECT<br />
id<br />
----------fruit---------<br />
,STUFF((SELECT DISTINCT ', ' + NULLIF(fruit,'')<br />
FROM tableToCSVRollup t2<br />
WHERE t1.id = t2.id<br />
FOR XML PATH(''),TYPE<br />
).value('.','VARCHAR(MAX)')<br />
,1,2,'') AS fruit<br />
----------city----------<br />
,STUFF((SELECT DISTINCT ', ' + NULLIF(city,'')<br />
FROM tableToCSVRollup t2<br />
WHERE t1.id = t2.id<br />
FOR XML PATH(''),TYPE<br />
).value('.','VARCHAR(MAX)')<br />
,1,2,'') AS city<br />
------------------------<br />
FROM tableToCSVRollup t1<br />
GROUP BY id<br />
ORDER BY id</code></p></blockquote>
<p>Results:</p>
<table border="2">
<tr>
<td><strong>id</strong></td>
<td><strong>fruit</strong></td>
<td><strong>city</strong></td>
</tr>
<tr>
<td>101</td>
<td>Apple, Banana</td>
<td>Cleveland, Pittsburgh</td>
</tr>
<tr>
<td>102</td>
<td>Grape, Melon</td>
<td>Cleveland</td>
</tr>
<tr>
<td>103</td>
<td>Melon</td>
<td>Cleveland, Pittsburgh</td>
</tr>
</table>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlslayer.com/wp/2009/11/06/aggregate-to-csv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

