<?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; ORDER BY Clause and TOP WITH TIES</title>
	<atom:link href="http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 17 May 2013 15:26:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Shekhar Teke, Sr DBA, Talkingtech Limited, NZ</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-407345</link>
		<dc:creator><![CDATA[Shekhar Teke, Sr DBA, Talkingtech Limited, NZ]]></dc:creator>
		<pubDate>Sun, 13 Jan 2013 22:23:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-407345</guid>
		<description><![CDATA[Hi Viren,

Hope you solve the problem. If not, here is the solution -

CREATE TABLE #TEMP
(ShowOnTop INT,
TypeId INT)

INSERT INTO #TEMP VALUES (2,1)
INSERT INTO #TEMP VALUES (3,2)
INSERT INTO #TEMP VALUES (0,2)
INSERT INTO #TEMP VALUES (1,2)
INSERT INTO #TEMP VALUES (0,1)
INSERT INTO #TEMP VALUES (0,3)
INSERT INTO #TEMP VALUES (0,1)

SELECT * FROM #TEMP

SELECT * FROM #TEMP
ORDER BY CASE WHEN ShowOnTop = 0 THEN 999999999 ELSE ShowOnTop END, TypeId

Put maximum number in CASE statement. Here I put 999999999.

Try this.

Cheers

Shekhar Teke
Sr DBA
Talkingtech Limited, NZ]]></description>
		<content:encoded><![CDATA[<p>Hi Viren,</p>
<p>Hope you solve the problem. If not, here is the solution -</p>
<p>CREATE TABLE #TEMP<br />
(ShowOnTop INT,<br />
TypeId INT)</p>
<p>INSERT INTO #TEMP VALUES (2,1)<br />
INSERT INTO #TEMP VALUES (3,2)<br />
INSERT INTO #TEMP VALUES (0,2)<br />
INSERT INTO #TEMP VALUES (1,2)<br />
INSERT INTO #TEMP VALUES (0,1)<br />
INSERT INTO #TEMP VALUES (0,3)<br />
INSERT INTO #TEMP VALUES (0,1)</p>
<p>SELECT * FROM #TEMP</p>
<p>SELECT * FROM #TEMP<br />
ORDER BY CASE WHEN ShowOnTop = 0 THEN 999999999 ELSE ShowOnTop END, TypeId</p>
<p>Put maximum number in CASE statement. Here I put 999999999.</p>
<p>Try this.</p>
<p>Cheers</p>
<p>Shekhar Teke<br />
Sr DBA<br />
Talkingtech Limited, NZ</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shekhar Teke, Sr DBA, Talkingtech Limited, NZ</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-407335</link>
		<dc:creator><![CDATA[Shekhar Teke, Sr DBA, Talkingtech Limited, NZ]]></dc:creator>
		<pubDate>Sun, 13 Jan 2013 21:42:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-407335</guid>
		<description><![CDATA[Hi Vishal,

Here is the solution -

CREATE TABLE #Temp
(Id INT,
Name varchar(3),
Parent INT)

INSERT INTO #Temp VALUES (1,&#039;ABC&#039;,Null)
INSERT INTO #Temp VALUES (2,&#039;PQR&#039;, 1)
INSERT INTO #Temp VALUES (3,&#039;DEF&#039;, Null)
INSERT INTO #Temp VALUES (4,&#039;LMN&#039;, Null)
INSERT INTO #Temp VALUES (5,&#039;OPQ&#039;, Null)
INSERT INTO #Temp VALUES (6,&#039;JKL&#039;, 2)
INSERT INTO #Temp VALUES (7,&#039;TUY&#039;, 4)

SELECT * FROM #Temp

SELECT * FROM #Temp ORDER BY Coalesce(Parent,Id)]]></description>
		<content:encoded><![CDATA[<p>Hi Vishal,</p>
<p>Here is the solution -</p>
<p>CREATE TABLE #Temp<br />
(Id INT,<br />
Name varchar(3),<br />
Parent INT)</p>
<p>INSERT INTO #Temp VALUES (1,&#8217;ABC&#8217;,Null)<br />
INSERT INTO #Temp VALUES (2,&#8217;PQR&#8217;, 1)<br />
INSERT INTO #Temp VALUES (3,&#8217;DEF&#8217;, Null)<br />
INSERT INTO #Temp VALUES (4,&#8217;LMN&#8217;, Null)<br />
INSERT INTO #Temp VALUES (5,&#8217;OPQ&#8217;, Null)<br />
INSERT INTO #Temp VALUES (6,&#8217;JKL&#8217;, 2)<br />
INSERT INTO #Temp VALUES (7,&#8217;TUY&#8217;, 4)</p>
<p>SELECT * FROM #Temp</p>
<p>SELECT * FROM #Temp ORDER BY Coalesce(Parent,Id)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #009 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-400676</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #009 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Sat, 29 Dec 2012 01:31:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-400676</guid>
		<description><![CDATA[[...] ORDER BY Clause and TOP WITH TIES [...]]]></description>
		<content:encoded><![CDATA[<p>[...] ORDER BY Clause and TOP WITH TIES [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viren Chheda</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-311712</link>
		<dc:creator><![CDATA[Viren Chheda]]></dc:creator>
		<pubDate>Tue, 10 Jul 2012 07:13:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-311712</guid>
		<description><![CDATA[Hi Pinal,
I&#039;m Viren from Mumbai.

I want to write 1 complex order by clause, plz help.

in Table A,  there&#039;s a column called ShowOnTop which contain either sort order like 1, 2, 3, ... or 0 by default. Another column called Type_Id which contains only 1, 2, 3.
I want to order my query such that first rows would be sorted according to ShowOnTop , den by Type_Id. But if in ShowOnTop, contains 0, den they shld be shown below Type_Id.

Ex:
Row  ShowOnTop  Type_Id
 1         2                    1
 2         3                    2
 3         0                    2
 4         1                    2
 5         0                    1
 6         0                    3
 7         0                    1

Data displayed would be:
4
1
2
5
7
3
6]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,<br />
I&#8217;m Viren from Mumbai.</p>
<p>I want to write 1 complex order by clause, plz help.</p>
<p>in Table A,  there&#8217;s a column called ShowOnTop which contain either sort order like 1, 2, 3, &#8230; or 0 by default. Another column called Type_Id which contains only 1, 2, 3.<br />
I want to order my query such that first rows would be sorted according to ShowOnTop , den by Type_Id. But if in ShowOnTop, contains 0, den they shld be shown below Type_Id.</p>
<p>Ex:<br />
Row  ShowOnTop  Type_Id<br />
 1         2                    1<br />
 2         3                    2<br />
 3         0                    2<br />
 4         1                    2<br />
 5         0                    1<br />
 6         0                    3<br />
 7         0                    1</p>
<p>Data displayed would be:<br />
4<br />
1<br />
2<br />
5<br />
7<br />
3<br />
6</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Swaminathan</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-287436</link>
		<dc:creator><![CDATA[Swaminathan]]></dc:creator>
		<pubDate>Wed, 23 May 2012 07:07:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-287436</guid>
		<description><![CDATA[Hello Pinal, 

As per the statement ( it will look for the column used in the ORDER BY to compare its equivalent in rest of the table with the *last row* of the results returned)

SELECT TOP 74956 WITH TIES *
FROM Sales.SalesOrderDetail
ORDER BY OrderQty
GO

the last row of the results returned will be with the OrderQty=2, for which the equivalent is 14200 rows, 

The actual result set returned is 74954 + 14200 = 89154 records
Can you please explain further.

Thanks,]]></description>
		<content:encoded><![CDATA[<p>Hello Pinal, </p>
<p>As per the statement ( it will look for the column used in the ORDER BY to compare its equivalent in rest of the table with the *last row* of the results returned)</p>
<p>SELECT TOP 74956 WITH TIES *<br />
FROM Sales.SalesOrderDetail<br />
ORDER BY OrderQty<br />
GO</p>
<p>the last row of the results returned will be with the OrderQty=2, for which the equivalent is 14200 rows, </p>
<p>The actual result set returned is 74954 + 14200 = 89154 records<br />
Can you please explain further.</p>
<p>Thanks,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AbdulKuddus K</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-276887</link>
		<dc:creator><![CDATA[AbdulKuddus K]]></dc:creator>
		<pubDate>Tue, 17 Apr 2012 07:02:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-276887</guid>
		<description><![CDATA[Nice reply]]></description>
		<content:encoded><![CDATA[<p>Nice reply</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Varsharani</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-255692</link>
		<dc:creator><![CDATA[Varsharani]]></dc:creator>
		<pubDate>Thu, 23 Feb 2012 12:11:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-255692</guid>
		<description><![CDATA[Thanks...!!]]></description>
		<content:encoded><![CDATA[<p>Thanks&#8230;!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vishal</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-230542</link>
		<dc:creator><![CDATA[Vishal]]></dc:creator>
		<pubDate>Tue, 03 Jan 2012 08:59:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-230542</guid>
		<description><![CDATA[Hello Pinal,

I have table with child and parent hierarchy within same table.
I hvae column to identify the parent of the respective node.
Records like

Id  name   Parent
1   ABC     Null
2   PQR     1
3   DEF     Null
4   LMN     Null
5   OPQ     Null
6   JKL      2
7   TUY     4

So i want to display parent &amp; then his child. if no child the next parent
Output expected is 

Id  name   Parent
1   ABC     Null
2   PQR     1
6   JKL      2
3   DEF     Null
4   LMN     Null
7   TUY     4
5   OPQ     Null

Please give the query providing the expected output parent following expected output.

Regards,
Vishal Shinde.]]></description>
		<content:encoded><![CDATA[<p>Hello Pinal,</p>
<p>I have table with child and parent hierarchy within same table.<br />
I hvae column to identify the parent of the respective node.<br />
Records like</p>
<p>Id  name   Parent<br />
1   ABC     Null<br />
2   PQR     1<br />
3   DEF     Null<br />
4   LMN     Null<br />
5   OPQ     Null<br />
6   JKL      2<br />
7   TUY     4</p>
<p>So i want to display parent &amp; then his child. if no child the next parent<br />
Output expected is </p>
<p>Id  name   Parent<br />
1   ABC     Null<br />
2   PQR     1<br />
6   JKL      2<br />
3   DEF     Null<br />
4   LMN     Null<br />
7   TUY     4<br />
5   OPQ     Null</p>
<p>Please give the query providing the expected output parent following expected output.</p>
<p>Regards,<br />
Vishal Shinde.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prakash</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-139414</link>
		<dc:creator><![CDATA[Prakash]]></dc:creator>
		<pubDate>Tue, 07 Jun 2011 10:19:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-139414</guid>
		<description><![CDATA[Hi pinal dave,

How to retrieve top 10 records which are recently updated?
and]]></description>
		<content:encoded><![CDATA[<p>Hi pinal dave,</p>
<p>How to retrieve top 10 records which are recently updated?<br />
and</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-133815</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Tue, 10 May 2011 12:43:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-133815</guid>
		<description><![CDATA[Search for Pagination in this site]]></description>
		<content:encoded><![CDATA[<p>Search for Pagination in this site</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: manohar</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-133784</link>
		<dc:creator><![CDATA[manohar]]></dc:creator>
		<pubDate>Tue, 10 May 2011 08:00:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-133784</guid>
		<description><![CDATA[how to retrieve top 100 records to top 1000 records from single table]]></description>
		<content:encoded><![CDATA[<p>how to retrieve top 100 records to top 1000 records from single table</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jyotika</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-106865</link>
		<dc:creator><![CDATA[Jyotika]]></dc:creator>
		<pubDate>Mon, 20 Dec 2010 12:27:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-106865</guid>
		<description><![CDATA[Thanks for explaining with ties clause.I was struggling with it.]]></description>
		<content:encoded><![CDATA[<p>Thanks for explaining with ties clause.I was struggling with it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wayne Erfling</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-80083</link>
		<dc:creator><![CDATA[Wayne Erfling]]></dc:creator>
		<pubDate>Tue, 13 Jul 2010 13:35:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-80083</guid>
		<description><![CDATA[When breaking a table into chunks using TOP WITH TIES, the obvious question is: 

How do I capture (output) the last value of the sort column so that I can begin the next group at the next value?

I&#039;m looking at the OUTPUT clause and the ranking functions today, but haven&#039;t reached any conclusions yet.]]></description>
		<content:encoded><![CDATA[<p>When breaking a table into chunks using TOP WITH TIES, the obvious question is: </p>
<p>How do I capture (output) the last value of the sort column so that I can begin the next group at the next value?</p>
<p>I&#8217;m looking at the OUTPUT clause and the ranking functions today, but haven&#8217;t reached any conclusions yet.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravindra</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-77898</link>
		<dc:creator><![CDATA[Ravindra]]></dc:creator>
		<pubDate>Mon, 28 Jun 2010 12:20:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-77898</guid>
		<description><![CDATA[hi,
        while transaction can we need to allocate any memory?

thanks and regards
ravindra]]></description>
		<content:encoded><![CDATA[<p>hi,<br />
        while transaction can we need to allocate any memory?</p>
<p>thanks and regards<br />
ravindra</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: k vamsi</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-59875</link>
		<dc:creator><![CDATA[k vamsi]]></dc:creator>
		<pubDate>Mon, 18 Jan 2010 06:06:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-59875</guid>
		<description><![CDATA[while fetching records, you are using the top keyword to fetch the records, say top 10 records and there is a eleventh record which matches the condition but due to top 10 condition that record has been dropped from the result set So in order to get that record you can use the keyword TIES.]]></description>
		<content:encoded><![CDATA[<p>while fetching records, you are using the top keyword to fetch the records, say top 10 records and there is a eleventh record which matches the condition but due to top 10 condition that record has been dropped from the result set So in order to get that record you can use the keyword TIES.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geetika</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-58961</link>
		<dc:creator><![CDATA[Geetika]]></dc:creator>
		<pubDate>Wed, 23 Dec 2009 23:14:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-58961</guid>
		<description><![CDATA[In what real time scenario WITH TIES is used?]]></description>
		<content:encoded><![CDATA[<p>In what real time scenario WITH TIES is used?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramdas</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-58947</link>
		<dc:creator><![CDATA[Ramdas]]></dc:creator>
		<pubDate>Wed, 23 Dec 2009 14:44:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-58947</guid>
		<description><![CDATA[Hi Pinal,
Never used with ties clause, thanks for bringing out the feature/article and explanation.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,<br />
Never used with ties clause, thanks for bringing out the feature/article and explanation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Schutz</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-58943</link>
		<dc:creator><![CDATA[Dave Schutz]]></dc:creator>
		<pubDate>Wed, 23 Dec 2009 13:29:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-58943</guid>
		<description><![CDATA[So wether you use WITH TIES depends on what you expect to get out of your data.]]></description>
		<content:encoded><![CDATA[<p>So wether you use WITH TIES depends on what you expect to get out of your data.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vijayakumar</title>
		<link>http://blog.sqlauthority.com/2009/12/23/sql-server-order-by-clause-and-top-with-ties/#comment-58930</link>
		<dc:creator><![CDATA[Vijayakumar]]></dc:creator>
		<pubDate>Wed, 23 Dec 2009 05:08:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7456#comment-58930</guid>
		<description><![CDATA[Hi Pinal,

There is no difference in Execution plan


With Regards
Vijayakumar.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>There is no difference in Execution plan</p>
<p>With Regards<br />
Vijayakumar.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
