 




<?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: Select Value question in SSMS</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/select-value-question/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/select-value-question/</link>
	<description></description>
	<lastBuildDate>Fri, 24 May 2013 15:01:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: The Most-Watched IT Questions: June 28, 2011 - ITKE Community Blog</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/select-value-question/#comment-93704</link>
		<dc:creator>The Most-Watched IT Questions: June 28, 2011 - ITKE Community Blog</dc:creator>
		<pubDate>Tue, 28 Jun 2011 07:01:00 +0000</pubDate>
		<guid isPermaLink="false">#comment-93704</guid>
		<description><![CDATA[[...] 6. Kccrosser provided the approved answer to a select value question in SSMS. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] 6. Kccrosser provided the approved answer to a select value question in SSMS. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kccrosser</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/select-value-question/#comment-93339</link>
		<dc:creator>kccrosser</dc:creator>
		<pubDate>Thu, 16 Jun 2011 17:57:09 +0000</pubDate>
		<guid isPermaLink="false">#comment-93339</guid>
		<description><![CDATA[I am getting errors trying to put this in the answer box above...


The problem is that the Group By is applied prior to the aggregate (&quot;Max&quot;) function, and the Max function is thereby run on the result of the Group By, and you are including the &quot;Stamp_Date&quot; column in your Group By clause.

Essentially, every unique occurrence of the Group By fields is being constructed in the result set, and then the Max is applied to that result.

What you need to do is first obtain the max(stamp_date) value for the Field_Index values, then get the record data that match that combination.  This requires a little more complex query:

&lt;pre&gt;select
	al1.Stamp_Date, al1.log_index, al1.log_comment, al1.stamp_uid
from action_log al1
where
	al1.log_index = &#039;1007&#039; 
	and al1.doc_id = &#039;49107052FD224F68BF64DA79E36E583E&#039;
	and not exists (
		select 1 from action_log al2
		where al2.log_index = al1.log_index
			and al2.doc_id = al1.doc_id
			and al2.Stamp_Date &gt; al1.Stamp_Date
		)
&lt;/pre&gt;

This will retrieve the latest entry for each combination of log_index and doc_id, by finding the one with the highest Stamp_Date.]]></description>
		<content:encoded><![CDATA[<p>I am getting errors trying to put this in the answer box above&#8230;</p>
<p>The problem is that the Group By is applied prior to the aggregate (&#8220;Max&#8221;) function, and the Max function is thereby run on the result of the Group By, and you are including the &#8220;Stamp_Date&#8221; column in your Group By clause.</p>
<p>Essentially, every unique occurrence of the Group By fields is being constructed in the result set, and then the Max is applied to that result.</p>
<p>What you need to do is first obtain the max(stamp_date) value for the Field_Index values, then get the record data that match that combination.  This requires a little more complex query:</p>
<pre>select
	al1.Stamp_Date, al1.log_index, al1.log_comment, al1.stamp_uid
from action_log al1
where
	al1.log_index = '1007' 
	and al1.doc_id = '49107052FD224F68BF64DA79E36E583E'
	and not exists (
		select 1 from action_log al2
		where al2.log_index = al1.log_index
			and al2.doc_id = al1.doc_id
			and al2.Stamp_Date &gt; al1.Stamp_Date
		)
</pre>
<p>This will retrieve the latest entry for each combination of log_index and doc_id, by finding the one with the highest Stamp_Date.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sadams1</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/select-value-question/#comment-93303</link>
		<dc:creator>sadams1</dc:creator>
		<pubDate>Wed, 15 Jun 2011 21:05:03 +0000</pubDate>
		<guid isPermaLink="false">#comment-93303</guid>
		<description><![CDATA[select 
max(stamp_date) AS Stamp_Date,
log_index,
log_comment,
stamp_uid
from action_log
where log_index = &#039;1007&#039; and doc_id = &#039;49107052FD224F68BF64DA79E36E583E&#039;
group by stamp_date, log_index, log_comment, stamp_uid

If I only select the stamp_date sql returns just the max stamp_date.  but if I add in the other select fields I get all rows with log_index = &#039;1007&#039; for the doc_id]]></description>
		<content:encoded><![CDATA[<p>select<br />
max(stamp_date) AS Stamp_Date,<br />
log_index,<br />
log_comment,<br />
stamp_uid<br />
from action_log<br />
where log_index = &#8217;1007&#8242; and doc_id = &#8217;49107052FD224F68BF64DA79E36E583E&#8217;<br />
group by stamp_date, log_index, log_comment, stamp_uid</p>
<p>If I only select the stamp_date sql returns just the max stamp_date.  but if I add in the other select fields I get all rows with log_index = &#8217;1007&#8242; for the doc_id</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mrdenny</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/select-value-question/#comment-93298</link>
		<dc:creator>mrdenny</dc:creator>
		<pubDate>Wed, 15 Jun 2011 20:10:27 +0000</pubDate>
		<guid isPermaLink="false">#comment-93298</guid>
		<description><![CDATA[What&#039;s the query that you are running?]]></description>
		<content:encoded><![CDATA[<p>What&#8217;s the query that you are running?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 6/9 queries in 0.015 seconds using memcached
Object Caching 310/313 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-24 16:32:39 -->