<?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: Optimizing SQL that selects the max/min/etc from a group.</title>
	<atom:link href="http://www.microshell.com/database/sql/optimizing-sql-that-selects-the-maxminetc-from-a-group/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.microshell.com/database/sql/optimizing-sql-that-selects-the-maxminetc-from-a-group/</link>
	<description>Learn something share something</description>
	<lastBuildDate>Mon, 23 Jan 2012 10:40:22 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/sql/optimizing-sql-that-selects-the-maxminetc-from-a-group/comment-page-1/#comment-1201</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Wed, 04 Nov 2009 14:58:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=99#comment-1201</guid>
		<description>Hi Robert ... Just out of curiosity, when you say it doesn&#039;t work, does it give error message? I noticed a typo on your 2nd query above. It&#039;s near &quot;SourceListInt INNER JOIN (SELECT MAX&quot;. Notice that your table name is missing an l (el). Looks like there&#039;s a typo there.</description>
		<content:encoded><![CDATA[<p>Hi Robert &#8230; Just out of curiosity, when you say it doesn&#8217;t work, does it give error message? I noticed a typo on your 2nd query above. It&#8217;s near &#8220;SourceListInt INNER JOIN (SELECT MAX&#8221;. Notice that your table name is missing an l (el). Looks like there&#8217;s a typo there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert</title>
		<link>http://www.microshell.com/database/sql/optimizing-sql-that-selects-the-maxminetc-from-a-group/comment-page-1/#comment-1186</link>
		<dc:creator>Robert</dc:creator>
		<pubDate>Mon, 02 Nov 2009 20:58:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=99#comment-1186</guid>
		<description>Hi Maresa 
First of all thanks for your post. I&#039;m a newbie with SQL. I know a bit more building MSAccess Queries. I wanted to give it a try and learn Subqueries and some SQL. 

I use your example to try to replicate my case. It is a single table where I have information of contracts by supplier. In that table a material-supplier can have more than one entry (contracts past and current). I just want to pick the most recent date. In a sense this is similar to your example.

What puzzles me is that I created the first step like in your case (it worked); even I saved those instructions as a MSAccess query (I called it &quot;Query4&quot;). Here are the instructions:
SELECT 
   MAX(SourceListIntl.BDATU) AS ContractDate, SourceListIntl.MATNR, SourceListIntl.WERKS
FROM
SourceListIntl
GROUP BY
SourceListIntl.MATNR, SourceListIntl.WERKS;

Then I wrote the instructions as per your second SQL query, but MSAccess returns an error message (Syntax error in JOIN operation). Here is the code:

SELECT SourceListIntl.MATNR, SourceListIntl.WERKS, SourceListIntl.BDATU, SourceListIntl.LIFNR, SourceListIntl.EBELN
FROM 
SourceListInt INNER JOIN (SELECT MAX(SourceListIntl.BDATU) AS ContractDate, SourceListIntl.MATNR, SourceListIntl.WERKS
FROM
SourceListIntl
GROUP BY SourceListIntl.MATNR, SourceListIntl.WERKS
) AS LastDate
ON (SourceListIntl.MATNR= LastDate.MATNR) AND (SourceListIntl.WERKS= LastDate.WERKS) AND (SourceListIntl.BDATU= LastDate.ContractDate);
This does not works :-(

BUT If I substitute the SELECT MAX ... instructions by Query4 it works !!! Like this:

SELECT SourceListIntl.MATNR, SourceListIntl.WERKS, SourceListIntl.BDATU, SourceListIntl.LIFNR, SourceListIntl.EBELN
FROM SourceListIntl INNER JOIN Query4 ON (SourceListIntl.WERKS=Query4.WERKS) AND (SourceListIntl.MATNR=Query4.MATNR) AND (SourceListIntl.BDATU=Query4.ContractDate)
GROUP BY SourceListIntl.MATNR, SourceListIntl.WERKS, SourceListIntl.BDATU, SourceListIntl.LIFNR, SourceListIntl.EBELN;

This last set of instructions works but I would like it to work like yours 

Any comments will be appreciated

Thanks in advance

Robert</description>
		<content:encoded><![CDATA[<p>Hi Maresa<br />
First of all thanks for your post. I&#8217;m a newbie with SQL. I know a bit more building MSAccess Queries. I wanted to give it a try and learn Subqueries and some SQL. </p>
<p>I use your example to try to replicate my case. It is a single table where I have information of contracts by supplier. In that table a material-supplier can have more than one entry (contracts past and current). I just want to pick the most recent date. In a sense this is similar to your example.</p>
<p>What puzzles me is that I created the first step like in your case (it worked); even I saved those instructions as a MSAccess query (I called it &#8220;Query4&#8243;). Here are the instructions:<br />
SELECT<br />
   MAX(SourceListIntl.BDATU) AS ContractDate, SourceListIntl.MATNR, SourceListIntl.WERKS<br />
FROM<br />
SourceListIntl<br />
GROUP BY<br />
SourceListIntl.MATNR, SourceListIntl.WERKS;</p>
<p>Then I wrote the instructions as per your second SQL query, but MSAccess returns an error message (Syntax error in JOIN operation). Here is the code:</p>
<p>SELECT SourceListIntl.MATNR, SourceListIntl.WERKS, SourceListIntl.BDATU, SourceListIntl.LIFNR, SourceListIntl.EBELN<br />
FROM<br />
SourceListInt INNER JOIN (SELECT MAX(SourceListIntl.BDATU) AS ContractDate, SourceListIntl.MATNR, SourceListIntl.WERKS<br />
FROM<br />
SourceListIntl<br />
GROUP BY SourceListIntl.MATNR, SourceListIntl.WERKS<br />
) AS LastDate<br />
ON (SourceListIntl.MATNR= LastDate.MATNR) AND (SourceListIntl.WERKS= LastDate.WERKS) AND (SourceListIntl.BDATU= LastDate.ContractDate);<br />
This does not works <img src='http://www.microshell.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>BUT If I substitute the SELECT MAX &#8230; instructions by Query4 it works !!! Like this:</p>
<p>SELECT SourceListIntl.MATNR, SourceListIntl.WERKS, SourceListIntl.BDATU, SourceListIntl.LIFNR, SourceListIntl.EBELN<br />
FROM SourceListIntl INNER JOIN Query4 ON (SourceListIntl.WERKS=Query4.WERKS) AND (SourceListIntl.MATNR=Query4.MATNR) AND (SourceListIntl.BDATU=Query4.ContractDate)<br />
GROUP BY SourceListIntl.MATNR, SourceListIntl.WERKS, SourceListIntl.BDATU, SourceListIntl.LIFNR, SourceListIntl.EBELN;</p>
<p>This last set of instructions works but I would like it to work like yours </p>
<p>Any comments will be appreciated</p>
<p>Thanks in advance</p>
<p>Robert</p>
]]></content:encoded>
	</item>
</channel>
</rss>

