<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; TOP WITH TIES</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/top-with-ties/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>TOP WITH TIES</title>
		<link>http://www.youdidwhatwithtsql.com/top-with-ties/54</link>
		<comments>http://www.youdidwhatwithtsql.com/top-with-ties/54#comments</comments>
		<pubDate>Tue, 07 Apr 2009 19:37:13 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[top records]]></category>
		<category><![CDATA[TOP WITH TIES]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/top-with-ties/54</guid>
		<description><![CDATA[Not many people seem to be aware of the WITH TIES clause introduced in SQL Server 2005+. This simple feature is worth adding to your query armoury. In the words of BOL WITH TIES… “Specifies that additional rows be returned from the base result set with the same value in the ORDER BY columns appearing [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/top-with-ties/54">TOP WITH TIES</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Not many people seem to be aware of the <strong>WITH TIES </strong>clause introduced in SQL Server 2005+. This simple feature is worth adding to your query armoury. In the words of <a href="http://msdn.microsoft.com/en-us/library/ms130214.aspx" target="_blank">BOL</a> <strong>WITH TIES</strong>…</p>
<p><em>“Specifies that additional rows be returned from the base result set with the same value in the ORDER BY columns appearing as the last of the TOP n (PERCENT) rows. TOP&#8230;WITH TIES can be specified only in SELECT statements, and only if an ORDER BY clause is specified.”</em></p>
<p>Many of us would have been asked in our professional lives “<em>Give me the top n of &lt;whatever&gt;“</em> and would have automatically thought of using <strong>TOP</strong>. Lets take the following scenario…</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p54code5'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p545"><td class="code" id="p54code5"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> SalesPerson
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	CurrentMonthSales <span style="color: #0000FF;">MONEY</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #FF0000;">'0.00'</span>,
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Now insert some test data&#8230;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p54code6'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p546"><td class="code" id="p54code6"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> SalesPerson
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	CurrentMonthSales
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span> 
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Rhys'</span>,
	<span style="color: #FF0000;">'Campbell'</span>,
	<span style="color: #FF0000;">'1000.00'</span>
<span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> SalesPerson
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	CurrentMonthSales
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'John'</span>,
	<span style="color: #FF0000;">'Doe'</span>,
	<span style="color: #FF0000;">'3500.00'</span>
<span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> SalesPerson
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	CurrentMonthSales
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Joe'</span>,
	<span style="color: #FF0000;">'Bloggs'</span>,
	<span style="color: #FF0000;">'3500.00'</span>
<span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> SalesPerson
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	CurrentMonthSales
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Jane'</span>,
	<span style="color: #FF0000;">'Doe'</span>,
	<span style="color: #FF0000;">'3500.00'</span>
<span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> SalesPerson
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	CurrentMonthSales
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'John'</span>,
	<span style="color: #FF0000;">'Smith'</span>,
	<span style="color: #FF0000;">'5000.00'</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Now if the under pressure <a href="http://en.wikipedia.org/wiki/Database_administrator" target="_blank">DBA</a> was asked for the top 3 performing sales people this month they might blast out something like&#8230; </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p54code7'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p547"><td class="code" id="p54code7"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">3</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> SalesPerson
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> CurrentMonthSales <span style="color: #0000FF;">DESC</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="107" alt="image thumb TOP WITH TIES" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image-thumb.png" width="244" border="0" /></a> </p>
<p>In many circumstances this may not be an issue. But here it could cause one of our friends in sales to miss out on a bonus! So keep friendly with sales and use the <strong>WITH TIES</strong>&#160; clause when appropriate.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p54code8'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p548"><td class="code" id="p54code8"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">3</span> <span style="color: #0000FF;">WITH</span> TIES <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> SalesPerson
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> CurrentMonthSales <span style="color: #0000FF;">DESC</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image1.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="105" alt="image thumb1 TOP WITH TIES" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/04/image-thumb1.png" width="244" border="0" /></a></p>
<p><map name='google_ad_map_54_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/54?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_54_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=54&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftop-with-ties%2F54' title="TOP WITH TIES" alt=" TOP WITH TIES" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/top-with-ties/54">TOP WITH TIES</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/top-with-ties/54" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/top-with-ties/54/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

