<?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: SQL SERVER - 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>
	<pubDate>Tue, 06 Jan 2009 12:34:40 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<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 "WorkingHours" it have three field (attributes) DayName, openhour,closehour(DataType to all is nvarchar(10)).

table records like this
1)"Monday","1000","2000"
2)"Sunday","0930","1930"
3)"Wednesday","1030","2030"

upto 7records, dayname are in disorder.But i want to dispaly the
day name in sequencial order like "Monday",Tuesday"....."Sunday"
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 - 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 - 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 &#60;&#62; 0 or
_max &#60;&#62; 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]) - 1 as bit) as _min,<br />
~cast(row_number() over (order by [field 1], [field 2], … [field n] desc) - 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'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; ) - 1 as bit) as _min,<br />
      ~cast(row_number() over (order by , , &#8230;  desc) - 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>
	<item>
		<title>By: Virendra</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34216</link>
		<dc:creator>Virendra</dc:creator>
		<pubDate>Mon, 10 Mar 2008 12:23:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34216</guid>
		<description>Hi, Pinal 
i likes your blog 
and i m giving a different solution, may be u like it

select max(colname) from Table
union
select min(colname) from Table
order by 1 desc


you will also see i have used  "order by 1 desc"
a technique to order by column in union</description>
		<content:encoded><![CDATA[<p>Hi, Pinal<br />
i likes your blog<br />
and i m giving a different solution, may be u like it</p>
<p>select max(colname) from Table<br />
union<br />
select min(colname) from Table<br />
order by 1 desc</p>
<p>you will also see i have used  &#8220;order by 1 desc&#8221;<br />
a technique to order by column in union</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim B</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34112</link>
		<dc:creator>Tim B</dc:creator>
		<pubDate>Tue, 04 Mar 2008 15:10:00 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34112</guid>
		<description>Performance-wise I like Dave and Khadar's solutions because they avoid using an IN clause.  But as you said, you were in a rush and the code worked just fine so there's nothing wrong with that!</description>
		<content:encoded><![CDATA[<p>Performance-wise I like Dave and Khadar&#8217;s solutions because they avoid using an IN clause.  But as you said, you were in a rush and the code worked just fine so there&#8217;s nothing wrong with that!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Khadar Khan</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34110</link>
		<dc:creator>Khadar Khan</dc:creator>
		<pubDate>Tue, 04 Mar 2008 11:43:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34110</guid>
		<description>Hi Pinal Dave,

I really thank you for spending your time on this query.

Regards
Khadar</description>
		<content:encoded><![CDATA[<p>Hi Pinal Dave,</p>
<p>I really thank you for spending your time on this query.</p>
<p>Regards<br />
Khadar</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-34101</link>
		<dc:creator>pinaldave</dc:creator>
		<pubDate>Tue, 04 Mar 2008 07:34:27 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34101</guid>
		<description>Hello Khadar Khan,

You are correct. I carefully observed my code and there was syntax error. Your code is correct and I like it. I will post it soon on this blog.

Working  example with adventureworks is here :
WITH TopBottomRow
AS
(
SELECT TOP 1 SalesOrderDetailID
        FROM Sales.SalesOrderDetail 
		ORDER BY SalesOrderDetailID 
UNION ALL
SELECT TOP 1 SalesOrderDetailID
        FROM Sales.SalesOrderDetail 
		ORDER BY SalesOrderDetailID DESC
)
SELECT * 
FROM TopBottomRow

Thank you again for your comment.

Regards,
Pinal Dave ( http://www.SQLAuthority.com )</description>
		<content:encoded><![CDATA[<p>Hello Khadar Khan,</p>
<p>You are correct. I carefully observed my code and there was syntax error. Your code is correct and I like it. I will post it soon on this blog.</p>
<p>Working  example with adventureworks is here :<br />
WITH TopBottomRow<br />
AS<br />
(<br />
SELECT TOP 1 SalesOrderDetailID<br />
        FROM Sales.SalesOrderDetail<br />
		ORDER BY SalesOrderDetailID<br />
UNION ALL<br />
SELECT TOP 1 SalesOrderDetailID<br />
        FROM Sales.SalesOrderDetail<br />
		ORDER BY SalesOrderDetailID DESC<br />
)<br />
SELECT *<br />
FROM TopBottomRow</p>
<p>Thank you again for your comment.</p>
<p>Regards,<br />
Pinal Dave ( <a href="http://www.SQLAuthority.com" rel="nofollow">http://www.SQLAuthority.com</a> )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Khadar Khan</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34100</link>
		<dc:creator>Khadar Khan</dc:creator>
		<pubDate>Tue, 04 Mar 2008 07:27:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34100</guid>
		<description>Hi Dave,

Thanks for the response.

After seeing your message, I cross checked the same on sql express , workgroup, dev edition and the enterprise editions to track this error. Everything is working fine and so far I never got this type of error in my application.
Not  sure why this error is popping up. I will check with other systems.

Thanks
Khadar</description>
		<content:encoded><![CDATA[<p>Hi Dave,</p>
<p>Thanks for the response.</p>
<p>After seeing your message, I cross checked the same on sql express , workgroup, dev edition and the enterprise editions to track this error. Everything is working fine and so far I never got this type of error in my application.<br />
Not  sure why this error is popping up. I will check with other systems.</p>
<p>Thanks<br />
Khadar</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-34099</link>
		<dc:creator>pinaldave</dc:creator>
		<pubDate>Tue, 04 Mar 2008 06:40:53 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34099</guid>
		<description>Khadar,

Your query generates following error :

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'asc'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'desc'.

Regards,
Pinal Dave ( http://www.SQLAuthority.com )</description>
		<content:encoded><![CDATA[<p>Khadar,</p>
<p>Your query generates following error :</p>
<p>Msg 156, Level 15, State 1, Line 5<br />
Incorrect syntax near the keyword &#8216;asc&#8217;.<br />
Msg 156, Level 15, State 1, Line 8<br />
Incorrect syntax near the keyword &#8216;desc&#8217;.</p>
<p>Regards,<br />
Pinal Dave ( <a href="http://www.SQLAuthority.com" rel="nofollow">http://www.SQLAuthority.com</a> )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Khadar Khan</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34098</link>
		<dc:creator>Khadar Khan</dc:creator>
		<pubDate>Tue, 04 Mar 2008 05:54:08 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34098</guid>
		<description>I am a regular reader of your blog and all your articles are excellent with real time scenarios. 
I had faced the same issue for displaying top and bottom rows and I used the below one with the help of CTE.
(ofcourse this query will work only in sql2005)

;with MyTab
as
(
	select top 1 * from request order by requestid asc
	union all
	select top 1 * from request order by requestid desc
)
select * from MyTab</description>
		<content:encoded><![CDATA[<p>I am a regular reader of your blog and all your articles are excellent with real time scenarios.<br />
I had faced the same issue for displaying top and bottom rows and I used the below one with the help of CTE.<br />
(ofcourse this query will work only in sql2005)</p>
<p>;with MyTab<br />
as<br />
(<br />
	select top 1 * from request order by requestid asc<br />
	union all<br />
	select top 1 * from request order by requestid desc<br />
)<br />
select * from MyTab</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Arthur</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34092</link>
		<dc:creator>Dave Arthur</dc:creator>
		<pubDate>Mon, 03 Mar 2008 17:09:51 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34092</guid>
		<description>How about this:

SELECT A.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey ASC) A
UNION ALL
SELECT B.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey DESC) B

and if you want to order the results, try:

SELECT C.* FROM (
	SELECT A.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey ASC) A
	UNION ALL
	SELECT B.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey DESC) B
) C ORDER BY C.SomeKey

--DA</description>
		<content:encoded><![CDATA[<p>How about this:</p>
<p>SELECT A.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey ASC) A<br />
UNION ALL<br />
SELECT B.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey DESC) B</p>
<p>and if you want to order the results, try:</p>
<p>SELECT C.* FROM (<br />
	SELECT A.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey ASC) A<br />
	UNION ALL<br />
	SELECT B.* FROM (SELECT TOP 1 * FROM DBO.SomeTable ORDER BY SomeTable.SomeKey DESC) B<br />
) C ORDER BY C.SomeKey</p>
<p>&#8211;DA</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alan Mojab</title>
		<link>http://blog.sqlauthority.com/2008/03/02/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql/#comment-34079</link>
		<dc:creator>Alan Mojab</dc:creator>
		<pubDate>Sun, 02 Mar 2008 15:51:34 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=524#comment-34079</guid>
		<description>I liked your approach to the problem.  I like any solution that works and is very easy to understand by anyone.

Don't make yourself worry about other possible solutions.</description>
		<content:encoded><![CDATA[<p>I liked your approach to the problem.  I like any solution that works and is very easy to understand by anyone.</p>
<p>Don&#8217;t make yourself worry about other possible solutions.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
