<?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: Retrieving N rows per group	</title>
	<atom:link href="https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/feed/" rel="self" type="application/rss+xml" />
	<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/</link>
	<description></description>
	<lastBuildDate>Thu, 01 Jan 2015 02:34:13 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>
	<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/#comment-2524</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Thu, 01 Jan 2015 02:34:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3407#comment-2524</guid>

					<description><![CDATA[Fixed now, Kevin. Thanks for pointing out my silly error.]]></description>
			<content:encoded><![CDATA[<p>Fixed now, Kevin. Thanks for pointing out my silly error.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/#comment-2523</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Wed, 31 Dec 2014 23:46:08 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3407#comment-2523</guid>

					<description><![CDATA[Yes - my focus on that last query was to demonstrate the impact of the residual predicate. I&#039;ll tweak the post at some point soon.]]></description>
			<content:encoded><![CDATA[<p>Yes &#8211; my focus on that last query was to demonstrate the impact of the residual predicate. I&#8217;ll tweak the post at some point soon.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: TheSQLGuru		</title>
		<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/#comment-2522</link>

		<dc:creator><![CDATA[TheSQLGuru]]></dc:creator>
		<pubDate>Wed, 31 Dec 2014 22:30:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3407#comment-2522</guid>

					<description><![CDATA[Rob, I am pretty sure your final query has a very common logic flaw that is causing incorrect results. You are filtering in the WHERE clause based on the LEFT JOINed table. This restricts the rows improperly, giving 961 rows of output instead of the expected 1045. &#160;Shouldn&#039;t the query be written thusly:
WITH Numbered AS
(
SELECT h.TransactionID, h.ProductID, h.TransactionDate, ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY TransactionDate DESC) AS RowNum
FROM Production.TransactionHistory h
)
SELECT p.Name, p.ProductID, t.TransactionID, t.TransactionDate
FROM Production.Product p
LEFT JOIN Numbered t ON t.ProductID = p.ProductID
AND t.RowNum &#060;= 5 * p.DaysToManufacture
WHERE p.Name &#062;= &#039;M&#039; AND p.Name &#060; &#039;S&#039;;
Or perhaps you intended to include only the WHERE clause rows? In this case an INNER JOIN would be more appropriate I think.]]></description>
			<content:encoded><![CDATA[<p>Rob, I am pretty sure your final query has a very common logic flaw that is causing incorrect results. You are filtering in the WHERE clause based on the LEFT JOINed table. This restricts the rows improperly, giving 961 rows of output instead of the expected 1045. &nbsp;Shouldn&#8217;t the query be written thusly:<br />
WITH Numbered AS<br />
(<br />
SELECT h.TransactionID, h.ProductID, h.TransactionDate, ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY TransactionDate DESC) AS RowNum<br />
FROM Production.TransactionHistory h<br />
)<br />
SELECT p.Name, p.ProductID, t.TransactionID, t.TransactionDate<br />
FROM Production.Product p<br />
LEFT JOIN Numbered t ON t.ProductID = p.ProductID<br />
AND t.RowNum &lt;= 5 * p.DaysToManufacture<br />
WHERE p.Name &gt;= &#8216;M&#8217; AND p.Name &lt; &#8216;S&#8217;;<br />
Or perhaps you intended to include only the WHERE clause rows? In this case an INNER JOIN would be more appropriate I think.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/#comment-2521</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Thu, 25 Dec 2014 13:41:44 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3407#comment-2521</guid>

					<description><![CDATA[Yes - the index I&#039;m using there matches that.]]></description>
			<content:encoded><![CDATA[<p>Yes &#8211; the index I&#8217;m using there matches that.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Uri Dimant		</title>
		<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/#comment-2520</link>

		<dc:creator><![CDATA[Uri Dimant]]></dc:creator>
		<pubDate>Thu, 25 Dec 2014 13:39:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3407#comment-2520</guid>

					<description><![CDATA[Hi Rob - Mery Christmas !
We want eliminate sorts by having an appropriate index. I like the concept POC introduced by Itzik Be-Gan to have an index on F (filtering) P (Partition) O (Orders) and C - Covering &#160;:-)))
Thanks]]></description>
			<content:encoded><![CDATA[<p>Hi Rob &#8211; Mery Christmas !<br />
We want eliminate sorts by having an appropriate index. I like the concept POC introduced by Itzik Be-Gan to have an index on F (filtering) P (Partition) O (Orders) and C &#8211; Covering &nbsp;:-)))<br />
Thanks</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/#comment-2519</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Thu, 25 Dec 2014 12:38:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3407#comment-2519</guid>

					<description><![CDATA[Hi Uri - Merry Christmas!
Yes, your mileage can vary depending on various factors. However, you should still be wary of Sort operators, and it&#039;s good to be aware of the options. I generally prefer APPLY, but have been known to use both.
Rob]]></description>
			<content:encoded><![CDATA[<p>Hi Uri &#8211; Merry Christmas!<br />
Yes, your mileage can vary depending on various factors. However, you should still be wary of Sort operators, and it&#8217;s good to be aware of the options. I generally prefer APPLY, but have been known to use both.<br />
Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Uri Dimant		</title>
		<link>https://lobsterpot.com.au/blog/2014/12/24/retrieving-n-rows-per-group/#comment-2518</link>

		<dc:creator><![CDATA[Uri Dimant]]></dc:creator>
		<pubDate>Thu, 25 Dec 2014 10:23:01 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3407#comment-2518</guid>

					<description><![CDATA[Hi Rob
If the dens is high,for example we have 10 customers with 100000000 orders then probably using cross apply which produces seek (if index exists ) will be optimal option.. On the other hand, row number will be better to have a scan if the dens is low..Just my two cents]]></description>
			<content:encoded><![CDATA[<p>Hi Rob<br />
If the dens is high,for example we have 10 customers with 100000000 orders then probably using cross apply which produces seek (if index exists ) will be optimal option.. On the other hand, row number will be better to have a scan if the dens is low..Just my two cents</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
