<?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 &#8211; Part 2</title>
	<atom:link href="http://blog.sqlauthority.com/2008/03/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/03/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/</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: F R</title>
		<link>http://blog.sqlauthority.com/2008/03/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/#comment-53703</link>
		<dc:creator>F R</dc:creator>
		<pubDate>Tue, 14 Jul 2009 23:19:32 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=532#comment-53703</guid>
		<description>This is an very helpful solution however,  can you show how we can do this with a Group By in the top and bottom queries.   It appears I cannot use an OrderBy  with a group by. .  

WITH TopBottomRow
AS
(
SELECT TOP 1 SalesOrderDetailID, Sum (Sales)
FROM Sales.SalesOrderDetail
Group By SalesOrder
ORDER BY SalesOrderDetailID
UNION ALL
SELECT TOP 1 SalesOrderDetailID, Sum(Sales)
FROM Sales.SalesOrderDetail
Group By SalesOrder
ORDER BY SalesOrderDetailID DESC
)
SELECT *
FROM TopBottomRow</description>
		<content:encoded><![CDATA[<p>This is an very helpful solution however,  can you show how we can do this with a Group By in the top and bottom queries.   It appears I cannot use an OrderBy  with a group by. .  </p>
<p>WITH TopBottomRow<br />
AS<br />
(<br />
SELECT TOP 1 SalesOrderDetailID, Sum (Sales)<br />
FROM Sales.SalesOrderDetail<br />
Group By SalesOrder<br />
ORDER BY SalesOrderDetailID<br />
UNION ALL<br />
SELECT TOP 1 SalesOrderDetailID, Sum(Sales)<br />
FROM Sales.SalesOrderDetail<br />
Group By SalesOrder<br />
ORDER BY SalesOrderDetailID DESC<br />
)<br />
SELECT *<br />
FROM TopBottomRow</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: roshan</title>
		<link>http://blog.sqlauthority.com/2008/03/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/#comment-43567</link>
		<dc:creator>roshan</dc:creator>
		<pubDate>Tue, 07 Oct 2008 10:49:28 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=532#comment-43567</guid>
		<description>how to retrieve a particular column.</description>
		<content:encoded><![CDATA[<p>how to retrieve a particular column.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nimesh Khatri</title>
		<link>http://blog.sqlauthority.com/2008/03/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/#comment-41834</link>
		<dc:creator>Nimesh Khatri</dc:creator>
		<pubDate>Wed, 20 Aug 2008 18:17:15 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=532#comment-41834</guid>
		<description>Min Max solution would not work for retrieving more than top one or bottom row. The original solution however will be able to retrieve the top 10 as well as bottom 10. 

Deep.... There can be multiple scenarios for this. If you want to add values from existing table then you will have to write a statement like....

INSERT INTO TABLE_NAME SELECT EXISTING_ROWS FROM ANOTHER_TABLE/TABLE_NAME

To insert static values, you could do something like this....

INSERT INTO TABLE_NAME
SELECT 1,2,3,4,5
UNION
SELECT 6,7,8,9,10</description>
		<content:encoded><![CDATA[<p>Min Max solution would not work for retrieving more than top one or bottom row. The original solution however will be able to retrieve the top 10 as well as bottom 10. </p>
<p>Deep&#8230;. There can be multiple scenarios for this. If you want to add values from existing table then you will have to write a statement like&#8230;.</p>
<p>INSERT INTO TABLE_NAME SELECT EXISTING_ROWS FROM ANOTHER_TABLE/TABLE_NAME</p>
<p>To insert static values, you could do something like this&#8230;.</p>
<p>INSERT INTO TABLE_NAME<br />
SELECT 1,2,3,4,5<br />
UNION<br />
SELECT 6,7,8,9,10</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: deep</title>
		<link>http://blog.sqlauthority.com/2008/03/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/#comment-41338</link>
		<dc:creator>deep</dc:creator>
		<pubDate>Tue, 05 Aug 2008 09:45:49 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=532#comment-41338</guid>
		<description>Hi ,
I have one query. How to add two row values togather in one table.</description>
		<content:encoded><![CDATA[<p>Hi ,<br />
I have one query. How to add two row values togather in one table.</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/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/#comment-39436</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>Mon, 23 Jun 2008 09:04:45 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=532#comment-39436</guid>
		<description>[...] up with alternate solution of the same problem. Khadar Khan came up with good solution using CTE SQL SERVER - How to Retrieve TOP and BOTTOM Rows Together using T-SQL - Part 2. Today we will see the solution suggested by Dave Arthur. This solution is quite good as it uses [...]</description>
		<content:encoded><![CDATA[<p>[...] up with alternate solution of the same problem. Khadar Khan came up with good solution using CTE SQL SERVER &#8211; How to Retrieve TOP and BOTTOM Rows Together using T-SQL &#8211; Part 2. Today we will see the solution suggested by Dave Arthur. This solution is quite good as it uses [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FSL</title>
		<link>http://blog.sqlauthority.com/2008/03/10/sql-server-how-to-retrieve-top-and-bottom-rows-together-using-t-sql-part-2/#comment-35218</link>
		<dc:creator>FSL</dc:creator>
		<pubDate>Sun, 20 Apr 2008 11:05:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=532#comment-35218</guid>
		<description>Thanks for sharing this.
I was used to do the same as follows:

SELECT * FROM
(
	(SELECT MIN(SalesOrderDetailID) AS ID
		FROM Sales.SalesOrderDetail
	)
	UNION
	(SELECT MAX(SalesOrderDetailID) AS ID
		FROM Sales.SalesOrderDetail
	)
) AS t_TopBottomRow;</description>
		<content:encoded><![CDATA[<p>Thanks for sharing this.<br />
I was used to do the same as follows:</p>
<p>SELECT * FROM<br />
(<br />
	(SELECT MIN(SalesOrderDetailID) AS ID<br />
		FROM Sales.SalesOrderDetail<br />
	)<br />
	UNION<br />
	(SELECT MAX(SalesOrderDetailID) AS ID<br />
		FROM Sales.SalesOrderDetail<br />
	)<br />
) AS t_TopBottomRow;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
