<?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; sql</title>
	<atom:link href="http://rabidgadfly.com/category/sql/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 View Bit Values in MySQL Query Browser</title>
		<link>http://rabidgadfly.com/2009/11/how-to-view-bit-values-in-mysql-query-browser/</link>
		<comments>http://rabidgadfly.com/2009/11/how-to-view-bit-values-in-mysql-query-browser/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 14:18:45 +0000</pubDate>
		<dc:creator>rabidgadfly</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.rabidgadfly.com/?p=123</guid>
		<description><![CDATA[I stumbled across this useful tidbit the other day in the MySQL Forums. I&#8217;ve always been frustrated that bit data type values fail to appear in MySQL Query Browser. Instead, it simply displays a &#8216;b&#8217; regardless of the actual value. I looked through all of the options but couldn&#8217;t find a setting related to the [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled across this useful tidbit the other day in the MySQL Forums. I&#8217;ve always been frustrated that bit data type values fail to appear in MySQL Query Browser. Instead, it simply displays a &#8216;b&#8217; regardless of the actual value.</p>
<p>I looked through all of the options but couldn&#8217;t find a setting related to the issue. The solution (workaround) turned out to be very easy. Just add 0 to the column in the SELECT list and the values will appear.</p>
<p>For example:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT myBooleanColumn + 0 as myBooleanColumn
FROM myTable
</pre>
<p>Either a 1 or a 0 will now appear as the value in the results.</p>
]]></content:encoded>
			<wfw:commentRss>http://rabidgadfly.com/2009/11/how-to-view-bit-values-in-mysql-query-browser/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
		<item>
		<title>Retrieving Records with Max Values using TOP</title>
		<link>http://rabidgadfly.com/2007/08/retrieving-records-with-max-values-using-top/</link>
		<comments>http://rabidgadfly.com/2007/08/retrieving-records-with-max-values-using-top/#comments</comments>
		<pubDate>Wed, 15 Aug 2007 14:46:08 +0000</pubDate>
		<dc:creator>rabidgadfly</dc:creator>
				<category><![CDATA[quickTip]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.rabidgadfly.com/?p=45</guid>
		<description><![CDATA[I know I know, it&#8217;s a horrible title. If anyone has a better suggestion please let me know! Here&#8217;s what I&#8217;m talking about: I have two tables; the first table contains unique records describing games and the second contains game ids, scores, and user ids. Game id is joining the first table to the second [...]]]></description>
			<content:encoded><![CDATA[<p>I know I know, it&#8217;s a horrible title. If anyone has a better suggestion please let me know!</p>
<p>Here&#8217;s what I&#8217;m talking about:</p>
<p>I have two tables; the first table contains unique records describing games and the second contains game ids, scores, and user ids. Game id is joining the first table to the second in a one to many, something like this:</p>
<p>Games<br />
======<br />
gameID<br />
gameName</p>
<p>GameScores<br />
=========<br />
gameID<br />
userID<br />
score</p>
<p>MY MISSION: Retrieve the user with the high score for each game in one query.</p>
<p>After much trial, error, and gnashing of teeth, the solution, as usual, proved to be ridiculously simple:</p>
<pre><code>
SELECT (select top 1 userID from GameScores where gameID = Games.gameID order by score desc) as topScoreUser,
                Games.gameName,Games.gameID, s.score
FROM Games
JOIN   GameScores s on s.gameID = Games.gameID
</code></pre>
<p>What this does is return one record for each game. Since I&#8217;m only returning the top record in the query and I&#8217;m ordering by score in descending order, I&#8217;m getting the top score and player for each game.</p>
<p>I&#8217;ve run into quite a few instances now where TOP has proved invaluable. Unfortunately it always seems to take me an hour of futzing around before I remember to use it!</p>
<p>-rG</p>
]]></content:encoded>
			<wfw:commentRss>http://rabidgadfly.com/2007/08/retrieving-records-with-max-values-using-top/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

