<?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>Greylurk &#187; subquery</title>
	<atom:link href="http://greylurk.com/index.php/tag/subquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://greylurk.com</link>
	<description>Ramblings of a distracted techie</description>
	<lastBuildDate>Tue, 06 Sep 2011 17:22:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Joining on Subqueries with SQL Server 2000+</title>
		<link>http://greylurk.com/index.php/2006/11/joining-on-subqueries-with-sql-server-2000/</link>
		<comments>http://greylurk.com/index.php/2006/11/joining-on-subqueries-with-sql-server-2000/#comments</comments>
		<pubDate>Mon, 27 Nov 2006 21:55:14 +0000</pubDate>
		<dc:creator>Adam N.</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[subquery]]></category>

		<guid isPermaLink="false">http://www.greylurk.com/wordpress/?p=17</guid>
		<description><![CDATA[Quite often I find myself wanting to get &#8220;the latest&#8221; result from a relational table. Normally this requires a subquery, which is slow. If I want more than one column from the relational table, I have to make multiple subqueries against a single table, which slows things down even more. However, recently, I discovered a [...]]]></description>
			<content:encoded><![CDATA[<p>Quite often I find myself wanting to get &#8220;the latest&#8221; result from a relational table.  Normally this requires a subquery, which is slow.  If I want more than one column from the relational table, I have to make multiple subqueries against a single table, which slows things down even more.  However, recently, I discovered a new method: joining against a subquery.</p>
<p>Say I have a list of events, and each one has a status report against it.  In my old scheme, I would have done something like this:</p>
<div style="background-color: #ffc; font: 10pt monospace; white-space: pre; border: 1px solid #ccc; margin: 2px 5px;overflow: auto;">SELECT e.id, e.title, e.date,</p>
<p>( SELECT TOP 1 r.Author FROM reports WHERE r.eventID = e.ID ORDER BY Date desc ) as ReportAuthor</p>
<p>( SELECT TOP 1 r.Date FROM reports WHERE r.eventID = e.ID ORDER BY Date desc ) as ReportDate</p>
<p>( SELECT TOP 1 r.Status FROM reports WHERE r.eventID = e.ID ORDER BY Date desc ) as ReportStatus</p>
<p>FROM Events e</p></div>
<p>However, the new way that I&#8217;ve found of doing things is:</p>
<div style="background-color: #ffc; font: 10pt monospace; white-space: pre; border: 1px solid #ccc; margin: 2px 5px;overflow: auto;">SELECT e.id, e.title, e.date, r.author as ReportAuthor, r.date as ReportDate, r.Status as reportStatus</p>
<p>FROM Events e</p>
<p>LEFT JOIN (</p>
<p>SELECT eventID, ReportDate, Author, Status</p>
<p>FROM Reports r1</p>
<p>WHERE ReportDate = (</p>
<p>SELECT TOP 1 ReportDate</p>
<p>FROM Reports R2</p>
<p>WHERE r1.eventID = R2.eventID</p>
<p>ORDER BY reportDate desc</p>
<p>)</p>
<p>ORDER BY Date desc</p>
<p>) r ON r.eventID = e.ID</p>
<p>WHERE &#8230;.</p></div>
<p>The second form executes significantly faster in the tests that I&#8217;ve done,</p>
<p><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;counturl=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;count=none&amp;text=Joining%20on%20Subqueries%20with%20SQL%20Server%202000%2B" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;counturl=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;count=none&amp;text=Joining%20on%20Subqueries%20with%20SQL%20Server%202000%2B" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;linkname=Joining%20on%20Subqueries%20with%20SQL%20Server%202000%2B" title="Delicious" rel="nofollow" target="_blank"><img src="http://greylurk.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_email" href="http://www.addtoany.com/add_to/email?linkurl=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;linkname=Joining%20on%20Subqueries%20with%20SQL%20Server%202000%2B" title="Email" rel="nofollow" target="_blank"><img src="http://greylurk.com/wp-content/plugins/add-to-any/icons/email.png" width="16" height="16" alt="Email"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fgreylurk.com%2Findex.php%2F2006%2F11%2Fjoining-on-subqueries-with-sql-server-2000%2F&amp;title=Joining%20on%20Subqueries%20with%20SQL%20Server%202000%2B" id="wpa2a_2"><img src="http://greylurk.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://greylurk.com/index.php/2006/11/joining-on-subqueries-with-sql-server-2000/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

