<?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>Jeremy Smyth's Blog &#187; Columns</title>
	<atom:link href="http://jeremysmyth.com/tag/columns/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeremysmyth.com</link>
	<description></description>
	<lastBuildDate>Sun, 04 Jul 2010 07:10:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tricks with ORDER BY</title>
		<link>http://jeremysmyth.com/2009/08/07/tricks-with-order-by/</link>
		<comments>http://jeremysmyth.com/2009/08/07/tricks-with-order-by/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 14:30:21 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[Columns]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=51</guid>
		<description><![CDATA[The ORDER BY clause in SQL Server is a bit more flexible than many people give it credit for.
In a training course I&#8217;m giving at the moment, we&#8217;ve just gone over a few of the lesser known uses, along with the widely used ones:

 The obvious: ordering by a column name


ORDER BY advance


 Less obvious: [...]]]></description>
			<content:encoded><![CDATA[<p>The <tt>ORDER BY</tt> clause in SQL Server is a bit more flexible than many people give it credit for.</p>
<p>In a training course I&#8217;m giving at the moment, we&#8217;ve just gone over a few of the lesser known uses, along with the widely used ones:</p>
<ul>
<li> The obvious: ordering by a column name
<pre>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">ORDER</span> <span class="kw1">BY</span> advance</div>
</div>
</pre>
<li> Less obvious: ordering by an expression on a column
<pre>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">ORDER</span> <span class="kw1">BY</span> id % <span class="nu0">2</span> <span class="co1">-- even IDs first, then odd</span></div>
</div>
</pre>
<li> Ordering by multiple fields
<pre>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">ORDER</span> <span class="kw1">BY</span> id % 2 <span class="kw1">DESC</span><span class="sy0">,</span> price 
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">-- odd orders first by even/odd IDs, then by prices within each</span></div>
</div>
</pre>
<li>Ordering by visible column position
<pre>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">SELECT</span> id<span class="sy0">,</span> customerid<span class="sy0">,</span> price <span class="sy0">*</span> quantity <span class="kw1">AS</span> <span class="st0">&quot;gross revenue&quot;</span>
<span class="kw1">FROM</span> orders
<span class="kw1">ORDER</span> <span class="kw1">BY</span> <span class="nu0">2</span></div>
</div>
</pre>
<li> Ordering by column aliases
<pre>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">SELECT</span> id<span class="sy0">,</span> customerid<span class="sy0">,</span> price <span class="sy0">*</span> quantity <span class="kw1">AS</span> <span class="st0">&quot;gross revenue&quot;</span>
<span class="kw1">FROM</span> orders
<span class="kw1">ORDER</span> <span class="kw1">BY</span> <span class="st0">&quot;gross revenue&quot;</span></div>
</div>
</pre>
</ul>
<p>The last one is particularly interesting, because column aliases can&#8217;t be used in other places e.g. in the where clause or from clause.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2009/08/07/tricks-with-order-by/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
