<?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>rabidGadfly &#187; delimited</title>
	<atom:link href="http://rabidgadfly.com/tag/delimited/feed/" rel="self" type="application/rss+xml" />
	<link>http://rabidgadfly.com</link>
	<description>Simple Solutions to Nagging Coding Problems</description>
	<lastBuildDate>Wed, 19 Oct 2011 13:43:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>How to Create Delimited Lists of Related Data in Your MS SQL Queries</title>
		<link>http://rabidgadfly.com/2009/01/how-to-create-delimited-lists-of-related-data-in-your-ms-sql-queries/</link>
		<comments>http://rabidgadfly.com/2009/01/how-to-create-delimited-lists-of-related-data-in-your-ms-sql-queries/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 19:37:07 +0000</pubDate>
		<dc:creator>rabidgadfly</dc:creator>
				<category><![CDATA[sql]]></category>
		<category><![CDATA[coalesce]]></category>
		<category><![CDATA[delimited]]></category>
		<category><![CDATA[group_concat]]></category>

		<guid isPermaLink="false">http://www.rabidgadfly.com/?p=98</guid>
		<description><![CDATA[Describes a technique for including comma-delimited lists from related tables as a column in your SQL result set.]]></description>
			<content:encoded><![CDATA[<p>A project I&#8217;m working on requires me to return asset records along with a comma-delimited list of related keywords. The keywords are stored in their own table and there is a one-to-many relationship from assets to keywords.</p>
<p>The following code returns what I&#8217;m looking for, but only for one record:</p>
<pre><code>
DECLARE @KeywordList varchar(1000)

SELECT @KeywordList = COALESCE(@KeywordList + ', ', '') + k.keyword
FROM assets a
JOIN keywords k ON a.asset_guid = k.asset_guid
WHERE a.asset_guid = 'xxxxxxxx-xxxx-xxxx-xxxx-023ECD4EBB4C'

SELECT @KeywordList
</code></pre>
<p>The problem was that I needed to return the results in a column for every record in the result set, and that code won&#8217;t work in a subquery.</p>
<p>My solution was to create a SQL user defined function.</p>
<p>Before proceeding, I should note that the method I&#8217;m posting should only be used if absolutely necessary. In addition to running your main query, you will also be running an additional query for every returned record, which can get pretty expensive.</p>
<p>But if you have a need as I did, this method should do the trick. Te following function accepts an asset GUID and returns the associated keywords in a comma-delimited list:</p>
<pre><code>
CREATE FUNCTION getDelimitedKeywords
   (@asset_guid uniqueidentifier )
RETURNS varchar(1000)
AS
BEGIN
   DECLARE @KeywordList varchar(1000)
   SELECT @KeywordList = COALESCE(@KeywordList + ', ', '') + keyword
   FROM keywords
   WHERE asset_guid = @asset_guid

   RETURN ( @KeywordList )
END
</code></pre>
<p>Now I can easily include the list in every row of my query by calling the function in my SELECT:</p>
<pre><code>
SELECT asset_name, dbo.getDelimitedKeywords(asset_guid) as Keywords
FROM assets
</code></pre>
<p>I didn&#8217;t research how to achieve the same results in mySQL, but it&#8217;s probably pretty easy to do using the GROUP_CONCAT function.</p>
<p>-rG</p>
]]></content:encoded>
			<wfw:commentRss>http://rabidgadfly.com/2009/01/how-to-create-delimited-lists-of-related-data-in-your-ms-sql-queries/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

