<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Return the Earliest Date Record in SQL Server 2005</title>
	<atom:link href="http://www.mindgravy.net/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mindgravy.net/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/</link>
	<description>Slather Your Mind</description>
	<lastBuildDate>Fri, 28 May 2010 04:00:41 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Steve</title>
		<link>http://www.mindgravy.net/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/comment-page-1/#comment-76</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Tue, 15 Jul 2008 20:43:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.mindgravy.net/index.php/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/#comment-76</guid>
		<description>I updated the post with more information.</description>
		<content:encoded><![CDATA[<p>I updated the post with more information.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Brooks</title>
		<link>http://www.mindgravy.net/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/comment-page-1/#comment-75</link>
		<dc:creator>Marc Brooks</dc:creator>
		<pubDate>Tue, 15 Jul 2008 18:08:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.mindgravy.net/index.php/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/#comment-75</guid>
		<description>What you&#039;re looking for is a subselect to determine that this order date is the minimum order date for this order detail line.  I&#039;m still confused as to why there would ever be more than one order header for an order detail line though...

SELECT * FROM OrderHeader AS Later WHERE NOT EXISTS (SELECT * FROM OrderHeader AS Earlier WHERE Earlier.ID = Later.ID AND Earlier.OrderDate &lt; Later.OrderDate)

This will give you the earliest OrderHeader row for any matching ID value.  You can then join to this as an inner query.

SELECT U.Name, OD.*, OH.*
FROM OrderDetail AS OD 
INNER JOIN (SELECT * FROM OrderHeader AS Later WHERE NOT EXISTS (SELECT * FROM OrderHeader AS Earlier WHERE Earlier.ID = Later .ID AND Earlier.OrderDate &lt; Later .OrderDate)
) AS OH ON OD.OrderID = OH.ID
INNER JOIN Users AS U ON U.ID = OH.UserID
ON U.</description>
		<content:encoded><![CDATA[<p>What you&#8217;re looking for is a subselect to determine that this order date is the minimum order date for this order detail line.  I&#8217;m still confused as to why there would ever be more than one order header for an order detail line though&#8230;</p>
<p>SELECT * FROM OrderHeader AS Later WHERE NOT EXISTS (SELECT * FROM OrderHeader AS Earlier WHERE Earlier.ID = Later.ID AND Earlier.OrderDate &lt; Later.OrderDate)</p>
<p>This will give you the earliest OrderHeader row for any matching ID value.  You can then join to this as an inner query.</p>
<p>SELECT U.Name, OD.*, OH.*<br />
FROM OrderDetail AS OD<br />
INNER JOIN (SELECT * FROM OrderHeader AS Later WHERE NOT EXISTS (SELECT * FROM OrderHeader AS Earlier WHERE Earlier.ID = Later .ID AND Earlier.OrderDate &lt; Later .OrderDate)<br />
) AS OH ON OD.OrderID = OH.ID<br />
INNER JOIN Users AS U ON U.ID = OH.UserID<br />
ON U.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.mindgravy.net/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/comment-page-1/#comment-74</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Tue, 15 Jul 2008 18:06:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.mindgravy.net/index.php/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/#comment-74</guid>
		<description>Marc: 

1.  I didn&#039;t post the SQL because you don&#039;t have the schema to replicate, but I guess I could have just posted it.  I&#039;ll keep that in mind the next time.

2.  What I mean is the earliest record of the OrderHeader with a single OrderDetail record.  So the earliest order information with the detail being distinct.  So in the example above, I only want a single record being returned for &quot;Widget 1&quot;, based on the earliest OrderDate, returning the information that is in that earliest OrderHeader Record - then joining the OrderDetail to get that detailed information.</description>
		<content:encoded><![CDATA[<p>Marc: </p>
<p>1.  I didn&#8217;t post the SQL because you don&#8217;t have the schema to replicate, but I guess I could have just posted it.  I&#8217;ll keep that in mind the next time.</p>
<p>2.  What I mean is the earliest record of the OrderHeader with a single OrderDetail record.  So the earliest order information with the detail being distinct.  So in the example above, I only want a single record being returned for &#8220;Widget 1&#8243;, based on the earliest OrderDate, returning the information that is in that earliest OrderHeader Record &#8211; then joining the OrderDetail to get that detailed information.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Brooks</title>
		<link>http://www.mindgravy.net/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/comment-page-1/#comment-73</link>
		<dc:creator>Marc Brooks</dc:creator>
		<pubDate>Tue, 15 Jul 2008 17:54:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.mindgravy.net/index.php/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/#comment-73</guid>
		<description>First, if you only post images, nobody can cut/paste your SQL to help.

Second, I&#039;m not sure what you mean by &quot;earliest record&quot; in the detail table, since it has no DATETIME fields.  How is it that you expect more than one header row for any particular detail record?</description>
		<content:encoded><![CDATA[<p>First, if you only post images, nobody can cut/paste your SQL to help.</p>
<p>Second, I&#8217;m not sure what you mean by &#8220;earliest record&#8221; in the detail table, since it has no DATETIME fields.  How is it that you expect more than one header row for any particular detail record?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.mindgravy.net/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/comment-page-1/#comment-72</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Tue, 15 Jul 2008 17:24:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.mindgravy.net/index.php/2008/07/15/return-the-earliest-date-record-in-sql-server-2005/#comment-72</guid>
		<description>Scott: If I do a group by, I can&#039;t include the CSR field because they are different.  How would I compare the order date to the Min date of the OrderHeader table and still return the rest of the records?

I did try to do this with a PARTITION BY clause, but the overhead is too much without putting it in a stored procedure.</description>
		<content:encoded><![CDATA[<p>Scott: If I do a group by, I can&#8217;t include the CSR field because they are different.  How would I compare the order date to the Min date of the OrderHeader table and still return the rest of the records?</p>
<p>I did try to do this with a PARTITION BY clause, but the overhead is too much without putting it in a stored procedure.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
