<?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>Curtis Summers &#187; sql</title>
	<atom:link href="http://csummers.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://csummers.com</link>
	<description>%w{life code ruby}.map { &#124;i&#124; "#{i} is awesome" }</description>
	<lastBuildDate>Sat, 31 Mar 2012 19:54:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PL/Ruby and PL/PHP in PostgreSQL</title>
		<link>http://csummers.com/2005/08/23/plruby-and-plphp-in-postgresql/</link>
		<comments>http://csummers.com/2005/08/23/plruby-and-plphp-in-postgresql/#comments</comments>
		<pubDate>Tue, 23 Aug 2005 16:08:44 +0000</pubDate>
		<dc:creator>Curt</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.csummers.org/?p=27</guid>
		<description><![CDATA[The ability to write procedural language (PL) in Ruby and PHP for PostgreSQL is awesome. I wish Oracle had something similar (and no, I&#8217;m not talking about Java). PL/Ruby PL/PHP]]></description>
			<content:encoded><![CDATA[<p>The ability to write procedural language (PL) in Ruby and PHP for PostgreSQL is awesome.  I wish Oracle had something similar (and no, I&#8217;m not talking about Java).</p>
<ul>
<li><a href="http://moulon.inra.fr/ruby/plruby.html">PL/Ruby</a></li>
<li><a href="http://www.commandprompt.com/community/plphp">PL/PHP</a></li>
</ul>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://csummers.com/2005/08/23/plruby-and-plphp-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pivoting data in SQL (cont.)</title>
		<link>http://csummers.com/2005/06/17/pivoting-data-in-sql-cont/</link>
		<comments>http://csummers.com/2005/06/17/pivoting-data-in-sql-cont/#comments</comments>
		<pubDate>Fri, 17 Jun 2005 20:50:07 +0000</pubDate>
		<dc:creator>Curt</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.csummers.org/?p=11</guid>
		<description><![CDATA[In my last sql post I showed a simple example of pivoting data in SQL when we know the values that we would like to pivot. Often, however, I run into the case where I must pivot data for which: I don&#8217;t know the values I need to pivot, or there are so many possible [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://www.csummers.org/index.php/2005/06/16/pivoting-data-in-sql/">sql post</a> I showed a simple example of pivoting data in SQL when we know the values that we would like to pivot.  Often, however, I run into the case where I must pivot data for which:</p>
<ol>
<li>I don&#8217;t know the values I need to pivot, or</li>
<li>there are so many possible values to pivot that I can&#8217;t possibly give each its own column</li>
</ol>
<p>A good example of this is summarizing Financial Aid awards by student for a particular term.  The university may have thousands of different awards to give out, but a student usually has no more than 10 awards per term.</p>
<p>So, my source table looks something like this:</p>
<pre>
TERM    ID    AWARD    AMOUNT
----    --    -----    ------
FA04     1     PELL      4050
FA04     1   ESCHOL       500
FA04     1   WKSTDY      2000
FA04     2     PELL      3500
FA04     2   WKSTDY      2000
FA04     3     PELL      4000
</pre>
<p>Since I can&#8217;t actually pivot the values into their own columns, I&#8217;m going to create AWARD columns 1..n containing the first <em>n</em> awards for each student per term.  In order to do this, I need to be able to number the awards per student, and then pivot the number.  Oracle&#8217;s RANK functionality makes this easy:</p>

<div class="wp_syntax"><div class="code"><pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span>
    TERM<span style="color: #66cc66;">,</span>
    ID<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN RN <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>    THEN AWARD
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>   AWARD_1<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN RN <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>    THEN AMOUNT
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>   AMOUNT_1<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN RN <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>    THEN AWARD
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>   AWARD_2<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN RN <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>    THEN AMOUNT
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>   AMOUNT_2<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN RN <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span>    THEN AWARD
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>   AWARD_3<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN RN <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span>    THEN AMOUNT
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span>   AMOUNT_3<span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>
         RANK<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> OVER<span style="color: #66cc66;">&#40;</span>PARTITION <span style="color: #993333; font-weight: bold;">BY</span>
                         TERM<span style="color: #66cc66;">,</span>
                         ID
                     <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
                         TERM<span style="color: #66cc66;">,</span>
                         ID<span style="color: #66cc66;">,</span>
                         AWARD<span style="color: #66cc66;">&#41;</span>  <span style="color: #993333; font-weight: bold;">AS</span> RN<span style="color: #66cc66;">,</span>
         AWARD<span style="color: #66cc66;">,</span>
         AMOUNT
     <span style="color: #993333; font-weight: bold;">FROM</span>
         AWARD_TABLE<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span>
    TERM<span style="color: #66cc66;">,</span>
    ID<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>And the results:</p>
<pre>
TERM  ID  AWARD_1  AMOUNT_1  AWARD_2  AMOUNT_2  AWARD_3  AMOUNT_3
----  --  -------  --------  -------  --------  -------  --------
FA04   1   ESCHOL       500     PELL      4050   WKSTDY      2000
FA04   2     PELL      3500   WKSTDY      2000
FA04   3     PELL      4000
</pre>
]]></content:encoded>
			<wfw:commentRss>http://csummers.com/2005/06/17/pivoting-data-in-sql-cont/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pivoting data in SQL</title>
		<link>http://csummers.com/2005/06/16/pivoting-data-in-sql/</link>
		<comments>http://csummers.com/2005/06/16/pivoting-data-in-sql/#comments</comments>
		<pubDate>Thu, 16 Jun 2005 17:02:46 +0000</pubDate>
		<dc:creator>Curt</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.csummers.org/?p=10</guid>
		<description><![CDATA[My job requires lots of fun SQL magic. Below is a technique I use frequently to pivot data in SQL. 1) Pivoting values based on a known value I often need to condense records listed in a table into one record per id by pivoting up some of the values into columns. A good example [...]]]></description>
			<content:encoded><![CDATA[<p>My job requires lots of fun SQL magic.  Below is a technique I use frequently to pivot data in SQL.</p>
<p>1) Pivoting values based on a known value</p>
<p>I often need to condense records listed in a table into one record per id by pivoting up some of the values into columns.  A good example would be a test score table like so:</p>
<pre>
ID    TEST_CODE    TEST_SCORE
--    ---------    ----------
 1          ACT            21
 2          ACT            21
 3          SAT          1200
 3          ACT            24
 4          ACT            19
 4          SAT          1010
 4          SAT          1100
</pre>
<p>Since I know the TEST_CODE values that I want to pivot, I can use CASE statements to pivot the values into ACT and SAT columns.  Also note that I&#8217;m grouping by ID and taking the MAX value of the specified test score.  If my table only has one test score per test type, then I could just as well use MIN and get the same result.  Otherwise, I&#8217;m electing to get the highest score available per person.</p>

<div class="wp_syntax"><div class="code"><pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span>
    ID<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN TEST_CODE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ACT'</span>
             THEN TEST_SCORE
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ACT_SCORE<span style="color: #66cc66;">,</span>
    MAX<span style="color: #66cc66;">&#40;</span>CASE WHEN TEST_CODE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'SAT'</span>
             THEN TEST_SCORE
             ELSE <span style="color: #993333; font-weight: bold;">NULL</span> END<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> SAT_SCORE
<span style="color: #993333; font-weight: bold;">FROM</span>
    TEST_TABLE
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span>
    ID;</pre></div></div>

<p>The results:</p>
<pre>
ID    ACT_SCORE    SAT_SCORE
--    ---------    ---------
 1           21
 2           21
 3           24         1200
 4           19         1100
</pre>
<p>Pretty simple, right?  Ok, but what if you need to pivot data and you don&#8217;t know the values that you want to pivot?  Or, what if the there are so many possible values to pivot that you can&#8217;t afford to give each one it&#8217;s own column?  I&#8217;ll answer that next time&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://csummers.com/2005/06/16/pivoting-data-in-sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

