<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; How to Retrieve TOP and BOTTOM Rows Together using T-SQL</title>
	<atom:link href="http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Martin</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-56253</link>
		<dc:creator>Martin</dc:creator>
		<pubDate>Tue, 29 Sep 2009 20:29:56 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-56253</guid>
		<description>I commiserate with all those who feel that if there is a TOP then there ought to be a BOTTOM.  If there is a UNION there ought to be a INTERSECTION.   Why does SQL have to be so lame?  Why can&#039;t I say &#039;select name from dog_table where dog_table.name = cat_table.name&#039;?, There is no ambiguity.  Why can&#039;t the interpreter convert my ints to strings automatically for a compare and just let me know that it did that if it is so concerned I might not get what I wanted in the first place?  Whose  bright idea was it to make everything equate to NULL? Null AND (anything) should be FALSE (except NULL and NULL which should be true), and NULL is not LIKE anything (except NULL). What is the difference beteen IS NULL and = NULL?     Why do some people still pronounce it Ess Que El, or sequel, when it should be monosyllabic as in what we do when we have to read through these posts in order to do what should be easy to do in the first place.  

SQueaL! SQL like a pig! 

Penal, your column has been most helpful for those of us who have no choice but to live within the muck that makes us SQL.

possible solution: 
change SQL language to support multiple adverbs for select:

select top 1, bottom 1:* from table;

Practical solution for now, read the prior responses, realize and accept that there is no BOTTOM in T-SQL as of this writing.

-Martin</description>
		<content:encoded><![CDATA[<p>I commiserate with all those who feel that if there is a TOP then there ought to be a BOTTOM.  If there is a UNION there ought to be a INTERSECTION.   Why does SQL have to be so lame?  Why can&#8217;t I say &#8217;select name from dog_table where dog_table.name = cat_table.name&#8217;?, There is no ambiguity.  Why can&#8217;t the interpreter convert my ints to strings automatically for a compare and just let me know that it did that if it is so concerned I might not get what I wanted in the first place?  Whose  bright idea was it to make everything equate to NULL? Null AND (anything) should be FALSE (except NULL and NULL which should be true), and NULL is not LIKE anything (except NULL). What is the difference beteen IS NULL and = NULL?     Why do some people still pronounce it Ess Que El, or sequel, when it should be monosyllabic as in what we do when we have to read through these posts in order to do what should be easy to do in the first place.  </p>
<p>SQueaL! SQL like a pig! </p>
<p>Penal, your column has been most helpful for those of us who have no choice but to live within the muck that makes us SQL.</p>
<p>possible solution:<br />
change SQL language to support multiple adverbs for select:</p>
<p>select top 1, bottom 1:* from table;</p>
<p>Practical solution for now, read the prior responses, realize and accept that there is no BOTTOM in T-SQL as of this writing.</p>
<p>-Martin</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MOK</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-54466</link>
		<dc:creator>MOK</dc:creator>
		<pubDate>Wed, 05 Aug 2009 10:03:49 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-54466</guid>
		<description>Thank you! Great article... solved my problem!</description>
		<content:encoded><![CDATA[<p>Thank you! Great article&#8230; solved my problem!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Manoj Mathe</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-53162</link>
		<dc:creator>Manoj Mathe</dc:creator>
		<pubDate>Mon, 22 Jun 2009 06:18:26 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-53162</guid>
		<description>HI,

I have a table where i am retriving the top1 record now i wnat to retrive a single column from the row and assign it to a local variable

declare @empid interger
select @empid top 1 empid from emp

but i am syntax error can anyone help me ove rthis</description>
		<content:encoded><![CDATA[<p>HI,</p>
<p>I have a table where i am retriving the top1 record now i wnat to retrive a single column from the row and assign it to a local variable</p>
<p>declare @empid interger<br />
select @empid top 1 empid from emp</p>
<p>but i am syntax error can anyone help me ove rthis</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kode</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-52901</link>
		<dc:creator>Kode</dc:creator>
		<pubDate>Wed, 10 Jun 2009 11:13:13 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-52901</guid>
		<description>Here’s a another solution to Retrieve TOP and BOTTOM Rows Together using T-SQL

select * from 
(
select top 1 * from emp order by sal desc 
union all
select top 1 * from emp order by sal asc)
as b</description>
		<content:encoded><![CDATA[<p>Here’s a another solution to Retrieve TOP and BOTTOM Rows Together using T-SQL</p>
<p>select * from<br />
(<br />
select top 1 * from emp order by sal desc<br />
union all<br />
select top 1 * from emp order by sal asc)<br />
as b</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PJ</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-52625</link>
		<dc:creator>PJ</dc:creator>
		<pubDate>Tue, 02 Jun 2009 02:15:48 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-52625</guid>
		<description>OK, apparently it&#039;s merely that my SQL Server 2008 database has some obscure setting I&#039;ve never heard of that is causing every two-dozen-variant experiments on this to fail. Sheesh. On a separate search result I found this note, when people complained of the same error:


&quot;Check the databse properties. DB compatibility might be set to 80. Row_Number function can only be used when compatibility is set to 90. You can check compatibility level by going to db properties and checking option tab.&quot;


I guess this is where being a web coder vs a DBA is a real problem. I just wasted like two days of my life trying to solve this problem as my queries are literally bringing my website down if I offer pagination.  So I apologize for ranting in the previous comment. I will go search for whatever setting on my shared server and home dev server might need to change that this will actually work for me.

However, massive google results saying &quot;there is no rownum equiv in sql-server&quot; did not help the confusion.

Best,
PJ</description>
		<content:encoded><![CDATA[<p>OK, apparently it&#8217;s merely that my SQL Server 2008 database has some obscure setting I&#8217;ve never heard of that is causing every two-dozen-variant experiments on this to fail. Sheesh. On a separate search result I found this note, when people complained of the same error:</p>
<p>&#8220;Check the databse properties. DB compatibility might be set to 80. Row_Number function can only be used when compatibility is set to 90. You can check compatibility level by going to db properties and checking option tab.&#8221;</p>
<p>I guess this is where being a web coder vs a DBA is a real problem. I just wasted like two days of my life trying to solve this problem as my queries are literally bringing my website down if I offer pagination.  So I apologize for ranting in the previous comment. I will go search for whatever setting on my shared server and home dev server might need to change that this will actually work for me.</p>
<p>However, massive google results saying &#8220;there is no rownum equiv in sql-server&#8221; did not help the confusion.</p>
<p>Best,<br />
PJ</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PJ</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-52624</link>
		<dc:creator>PJ</dc:creator>
		<pubDate>Tue, 02 Jun 2009 01:48:26 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-52624</guid>
		<description>It drives me crazy that google search is filled with a zillion responses specific to SQL server and the exact problem noted above -- not wanting to have to retrieve 250,000 records in order to just get from 200-300 of a specific sort order -- and the solutions posted DO NOT WORK IN SQL SERVER BECAUSE ROWNUM/ROW_NUMBER() IS NOT A RECOGNIZED FUNCTION NAME in that database. There is no equivalent, says hours of searching on that very question.

This page is specific to SQL Server yet there are solutions here that pointedly will not work in SQL Server. Yes, if only we had a rownum function, pagination would be so easy!  Instead we have long-lagging queries because they either have to get a ton of records and then use the middleware to restrict output, or they have to make multiple tables and queries and go back&amp;forth trying to &quot;back-end-hack&quot; a solution.

Sorry if I sound annoyed but after wasting my weekend reading search results and STILL not finding an answer, when this has been a question online for literally 9 years now!, is very frustrating!

A nice follow up post to your &#039;top and bottom&#039; entry might be, &#039;what if I need to get 10 records from &#039;somewhere in the middle&#039;?

PJ</description>
		<content:encoded><![CDATA[<p>It drives me crazy that google search is filled with a zillion responses specific to SQL server and the exact problem noted above &#8212; not wanting to have to retrieve 250,000 records in order to just get from 200-300 of a specific sort order &#8212; and the solutions posted DO NOT WORK IN SQL SERVER BECAUSE ROWNUM/ROW_NUMBER() IS NOT A RECOGNIZED FUNCTION NAME in that database. There is no equivalent, says hours of searching on that very question.</p>
<p>This page is specific to SQL Server yet there are solutions here that pointedly will not work in SQL Server. Yes, if only we had a rownum function, pagination would be so easy!  Instead we have long-lagging queries because they either have to get a ton of records and then use the middleware to restrict output, or they have to make multiple tables and queries and go back&amp;forth trying to &#8220;back-end-hack&#8221; a solution.</p>
<p>Sorry if I sound annoyed but after wasting my weekend reading search results and STILL not finding an answer, when this has been a question online for literally 9 years now!, is very frustrating!</p>
<p>A nice follow up post to your &#8216;top and bottom&#8217; entry might be, &#8216;what if I need to get 10 records from &#8217;somewhere in the middle&#8217;?</p>
<p>PJ</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jagadish</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-52085</link>
		<dc:creator>Jagadish</dc:creator>
		<pubDate>Wed, 20 May 2009 05:28:03 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-52085</guid>
		<description>Hi Dave,

I want to show or retrive data in some range
e.g
      top 20, then I will write &quot;select top 20 ...&quot;

But what if I want top 30 to 40 records, I want to skip top 1 to 30 and show 30 to 40 records
&quot;select top 30 to 40 ...&quot; will not work
one soluntion is I am using inner query,

&quot;select * from(select *,ROW_NUMBER() OVER(Order by FirstName DESC)as &#039;ui&#039; from emp) as temp where ui between 30 to 40&quot;
but I want to do this same using single query, so is it possible?
Please reply
Thanx

Jagadish Mori</description>
		<content:encoded><![CDATA[<p>Hi Dave,</p>
<p>I want to show or retrive data in some range<br />
e.g<br />
      top 20, then I will write &#8220;select top 20 &#8230;&#8221;</p>
<p>But what if I want top 30 to 40 records, I want to skip top 1 to 30 and show 30 to 40 records<br />
&#8220;select top 30 to 40 &#8230;&#8221; will not work<br />
one soluntion is I am using inner query,</p>
<p>&#8220;select * from(select *,ROW_NUMBER() OVER(Order by FirstName DESC)as &#8216;ui&#8217; from emp) as temp where ui between 30 to 40&#8243;<br />
but I want to do this same using single query, so is it possible?<br />
Please reply<br />
Thanx</p>
<p>Jagadish Mori</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nash</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-51730</link>
		<dc:creator>Nash</dc:creator>
		<pubDate>Sat, 09 May 2009 07:19:06 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-51730</guid>
		<description>Here&#039;s a another solution to Retrieve TOP and BOTTOM Rows Together using T-SQL

I hope I have done it...

SELECT *
FROM Sales.SalesOrderDetail WHERE SalesOrderDetailID IN
(
SELECT max(SalesOrderDetailID) FROM Sales.SalesOrderDetail
UNION ALL
SELECT min(SalesOrderDetailID) FROM Sales.SalesOrderDetail
)</description>
		<content:encoded><![CDATA[<p>Here&#8217;s a another solution to Retrieve TOP and BOTTOM Rows Together using T-SQL</p>
<p>I hope I have done it&#8230;</p>
<p>SELECT *<br />
FROM Sales.SalesOrderDetail WHERE SalesOrderDetailID IN<br />
(<br />
SELECT max(SalesOrderDetailID) FROM Sales.SalesOrderDetail<br />
UNION ALL<br />
SELECT min(SalesOrderDetailID) FROM Sales.SalesOrderDetail<br />
)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pratik Solanki</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-46709</link>
		<dc:creator>Pratik Solanki</dc:creator>
		<pubDate>Mon, 16 Feb 2009 01:07:08 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-46709</guid>
		<description>It&#039;s really helpfull to me. Thanks to all of you including Pinal.</description>
		<content:encoded><![CDATA[<p>It&#8217;s really helpfull to me. Thanks to all of you including Pinal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Faslu K V **Simplest Solution ever***</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-46246</link>
		<dc:creator>Faslu K V **Simplest Solution ever***</dc:creator>
		<pubDate>Mon, 02 Feb 2009 14:53:58 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-46246</guid>
		<description>select max(EmpID) as EmpID, &#039;Biggest&#039; as [Level] from employee
union all
select min(EMPID) as EmpID, &#039;Lowest&#039; as [level] from employee order by EmpID asc--&#039;order by&#039; can be added/changed as required</description>
		<content:encoded><![CDATA[<p>select max(EmpID) as EmpID, &#8216;Biggest&#8217; as [Level] from employee<br />
union all<br />
select min(EMPID) as EmpID, &#8216;Lowest&#8217; as [level] from employee order by EmpID asc&#8211;&#8217;order by&#8217; can be added/changed as required</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-45624</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 15 Jan 2009 04:27:40 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-45624</guid>
		<description>What do i do if i want to retreive the bottom 5 UnitPrice from the products table?</description>
		<content:encoded><![CDATA[<p>What do i do if i want to retreive the bottom 5 UnitPrice from the products table?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tarun</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-39206</link>
		<dc:creator>Tarun</dc:creator>
		<pubDate>Fri, 13 Jun 2008 06:38:10 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-39206</guid>
		<description>Hi

What TO Do if i want TOP or Bottom In Same Row?

I have this with Case statement But is there is any other Method pls tell me.

Regards

Tarun</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>What TO Do if i want TOP or Bottom In Same Row?</p>
<p>I have this with Case statement But is there is any other Method pls tell me.</p>
<p>Regards</p>
<p>Tarun</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: medi</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-37225</link>
		<dc:creator>medi</dc:creator>
		<pubDate>Wed, 07 May 2008 17:27:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-37225</guid>
		<description>Hello, i have e problem to translate er diagram  for constraint participation in sql statement , how do you have this thing with trigger or something else  i am doing this in SQL server 2005 ,if you have any solution please send me in my email for this problem good bye and good lucy for you.</description>
		<content:encoded><![CDATA[<p>Hello, i have e problem to translate er diagram  for constraint participation in sql statement , how do you have this thing with trigger or something else  i am doing this in SQL server 2005 ,if you have any solution please send me in my email for this problem good bye and good lucy for you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tamizh</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-35832</link>
		<dc:creator>Tamizh</dc:creator>
		<pubDate>Mon, 28 Apr 2008 14:24:32 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-35832</guid>
		<description>Hi Pinal Dave,

  i have one problem to get data from table.
My table name is &quot;WorkingHours&quot; it have three field (attributes) DayName, openhour,closehour(DataType to all is nvarchar(10)).

table records like this
1)&quot;Monday&quot;,&quot;1000&quot;,&quot;2000&quot;
2)&quot;Sunday&quot;,&quot;0930&quot;,&quot;1930&quot;
3)&quot;Wednesday&quot;,&quot;1030&quot;,&quot;2030&quot;

upto 7records, dayname are in disorder.But i want to dispaly the
day name in sequencial order like &quot;Monday&quot;,Tuesday&quot;.....&quot;Sunday&quot;
How can i write query to get day name in sequenciql order?</description>
		<content:encoded><![CDATA[<p>Hi Pinal Dave,</p>
<p>  i have one problem to get data from table.<br />
My table name is &#8220;WorkingHours&#8221; it have three field (attributes) DayName, openhour,closehour(DataType to all is nvarchar(10)).</p>
<p>table records like this<br />
1)&#8221;Monday&#8221;,&#8221;1000&#8243;,&#8221;2000&#8243;<br />
2)&#8221;Sunday&#8221;,&#8221;0930&#8243;,&#8221;1930&#8243;<br />
3)&#8221;Wednesday&#8221;,&#8221;1030&#8243;,&#8221;2030&#8243;</p>
<p>upto 7records, dayname are in disorder.But i want to dispaly the<br />
day name in sequencial order like &#8220;Monday&#8221;,Tuesday&#8221;&#8230;..&#8221;Sunday&#8221;<br />
How can i write query to get day name in sequenciql order?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Schroeder</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34457</link>
		<dc:creator>Jeff Schroeder</dc:creator>
		<pubDate>Wed, 19 Mar 2008 16:46:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34457</guid>
		<description>Great blog!  I totally forgot how to do this with the sub-query.  Thanks! :-)</description>
		<content:encoded><![CDATA[<p>Great blog!  I totally forgot how to do this with the sub-query.  Thanks! :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER - How to Retrieve TOP and BOTTOM Rows Together using T-SQL - Part 3 Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34263</link>
		<dc:creator>SQL SERVER - How to Retrieve TOP and BOTTOM Rows Together using T-SQL - Part 3 Journey to SQL Authority with Pinal Dave</dc:creator>
		<pubDate>Tue, 11 Mar 2008 18:33:49 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34263</guid>
		<description>[...] 11, 2008 by pinaldave    Please read SQL SERVER - How to Retrieve TOP and BOTTOM Rows Together using T-SQL before continuing this article. I had asked users to come up with alternate solution of the same [...]</description>
		<content:encoded><![CDATA[<p>[...] 11, 2008 by pinaldave    Please read SQL SERVER &#8211; How to Retrieve TOP and BOTTOM Rows Together using T-SQL before continuing this article. I had asked users to come up with alternate solution of the same [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34261</link>
		<dc:creator>pinaldave</dc:creator>
		<pubDate>Tue, 11 Mar 2008 18:23:42 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34261</guid>
		<description>Hello Nicholas Paldino,

I always enjoy your participation in this blog. Really very nice query and quite an enhancement to my original query.

Would you please create one working example using AdventureWorks database and I will post it on my blog with your courtesy.

Kind Regards,
Pinal Dave ( http://www.SQLAuthority.com )</description>
		<content:encoded><![CDATA[<p>Hello Nicholas Paldino,</p>
<p>I always enjoy your participation in this blog. Really very nice query and quite an enhancement to my original query.</p>
<p>Would you please create one working example using AdventureWorks database and I will post it on my blog with your courtesy.</p>
<p>Kind Regards,<br />
Pinal Dave ( <a href="http://www.SQLAuthority.com" rel="nofollow">http://www.SQLAuthority.com</a> )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER - How to Retrieve TOP and BOTTOM Rows Together using T-SQL - Part 2 Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34260</link>
		<dc:creator>SQL SERVER - How to Retrieve TOP and BOTTOM Rows Together using T-SQL - Part 2 Journey to SQL Authority with Pinal Dave</dc:creator>
		<pubDate>Tue, 11 Mar 2008 18:21:32 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34260</guid>
		<description>[...] 10, 2008 by pinaldave    Please read SQL SERVER - How to Retrieve TOP and BOTTOM Rows Together using T-SQL before continuing this article. I had asked users to come up with alternate solution of the same [...]</description>
		<content:encoded><![CDATA[<p>[...] 10, 2008 by pinaldave    Please read SQL SERVER &#8211; How to Retrieve TOP and BOTTOM Rows Together using T-SQL before continuing this article. I had asked users to come up with alternate solution of the same [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicholas Paldino [.NET/C# MVP]</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34224</link>
		<dc:creator>Nicholas Paldino [.NET/C# MVP]</dc:creator>
		<pubDate>Mon, 10 Mar 2008 16:04:29 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34224</guid>
		<description>Sorry, the query should be:

select
t.*
from
(
select
~cast(row_number() over (order by [field 1], [field 2], … [field n]) - 1 as bit) as _min,
~cast(row_number() over (order by [field 1], [field 2], … [field n] desc) - 1 as bit) as _max,
t.*
from
as t
) as t
where
_min &lt;&gt; 0 or
_max &lt;&gt; 0</description>
		<content:encoded><![CDATA[<p>Sorry, the query should be:</p>
<p>select<br />
t.*<br />
from<br />
(<br />
select<br />
~cast(row_number() over (order by [field 1], [field 2], … [field n]) &#8211; 1 as bit) as _min,<br />
~cast(row_number() over (order by [field 1], [field 2], … [field n] desc) &#8211; 1 as bit) as _max,<br />
t.*<br />
from<br />
as t<br />
) as t<br />
where<br />
_min &lt;&gt; 0 or<br />
_max &lt;&gt; 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicholas Paldino [.NET/C# MVP]</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34223</link>
		<dc:creator>Nicholas Paldino [.NET/C# MVP]</dc:creator>
		<pubDate>Mon, 10 Mar 2008 16:02:45 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34223</guid>
		<description>The problem with this is that it will only work if there is one column that is being applied in the sort.  If you need multiple columns, then the ROW_NUMBER ranking function is a better idea (and can be used in all cases):

select
  t.*
from
  (
    select
      ~cast(row_number() over (order by , , ... ) - 1 as bit) as _min,
      ~cast(row_number() over (order by , , ...  desc) - 1 as bit) as _max,
      t.* 
    from 
       as t
  ) as t
where
  _min  0 or
  _max  0

If you don&#039;t want the addition of the _min and _max fields to the result set, then it would be possible to do so using a number of methods, but the general idea is the same (and it would prevent a duplicate table scan as well due to the union).</description>
		<content:encoded><![CDATA[<p>The problem with this is that it will only work if there is one column that is being applied in the sort.  If you need multiple columns, then the ROW_NUMBER ranking function is a better idea (and can be used in all cases):</p>
<p>select<br />
  t.*<br />
from<br />
  (<br />
    select<br />
      ~cast(row_number() over (order by , , &#8230; ) &#8211; 1 as bit) as _min,<br />
      ~cast(row_number() over (order by , , &#8230;  desc) &#8211; 1 as bit) as _max,<br />
      t.*<br />
    from<br />
       as t<br />
  ) as t<br />
where<br />
  _min  0 or<br />
  _max  0</p>
<p>If you don&#8217;t want the addition of the _min and _max fields to the result set, then it would be possible to do so using a number of methods, but the general idea is the same (and it would prevent a duplicate table scan as well due to the union).</p>
]]></content:encoded>
	</item>
</channel>
</rss>
