<?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>dimitar.me &#187; SQL</title>
	<atom:link href="http://dimitar.me/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://dimitar.me</link>
	<description>Dimitar Darazhanski&#039;s blog.</description>
	<lastBuildDate>Wed, 01 Feb 2012 03:17:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Oracle &#8211; Change the behavior of ORDER BY</title>
		<link>http://dimitar.me/oracle-change-the-behavior-of-order-by/</link>
		<comments>http://dimitar.me/oracle-change-the-behavior-of-order-by/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 19:06:13 +0000</pubDate>
		<dc:creator>dimitar</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://dimitar.me/?p=72</guid>
		<description><![CDATA[There are times when you would like to order the results of a query in such a way that it cannot be accomplished by the default behavior of the order by clause. For example, let&#8217;s look at the following table: tbl_families FirstName LastName FamilyMember Mark Smith child John Smith husband Susan Smith wife Sophia Dice [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when you would like to order the results of a query in such a way that it cannot be accomplished by the default behavior of the <em>order by</em> clause. For example, let&#8217;s look at the following table:</p>
<p><strong>tbl_families</strong></p>
<table border="0">
<tbody>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>FamilyMember</th>
</tr>
<tr>
<td>Mark</td>
<td>Smith</td>
<td>child</td>
</tr>
<tr>
<td>John</td>
<td>Smith</td>
<td>husband</td>
</tr>
<tr>
<td>Susan</td>
<td>Smith</td>
<td>wife</td>
</tr>
<tr>
<td>Sophia</td>
<td>Dice</td>
<td>child</td>
</tr>
<tr>
<td>Craig</td>
<td>Dice</td>
<td>husband</td>
</tr>
<tr>
<td>Maria</td>
<td>Dice</td>
<td>wife</td>
</tr>
</tbody>
</table>
<p></p>
<p>Let&#8217;s say you need to order this table in such a way, so that you get all the husbands first, then the wives and the children at the end. Obviously, descending or ascending ordering by the <em>FamilyMember</em> field will not do the job. The solution is to combine the <em>order by</em> clause with the Oracle&#8217;s <em>decode</em> clause. Here is the result:</p>
<pre><code>SELECT FirstName, LastName, FamilyMember
FROM tbl_families
ORDER BY DECODE (FamilyMember, 'husband', 'A', 'wife', 'B', 'child', 'C', 'Z');</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://dimitar.me/oracle-change-the-behavior-of-order-by/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

