<?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/"
	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>SQL Brain</title>
	<atom:link href="http://sqlbrain.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlbrain.wordpress.com</link>
	<description>This is your Brain on SQL</description>
	<lastBuildDate>Fri, 02 Dec 2011 11:06:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sqlbrain.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>SQL Brain</title>
		<link>http://sqlbrain.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sqlbrain.wordpress.com/osd.xml" title="SQL Brain" />
	<atom:link rel='hub' href='http://sqlbrain.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Passing a variable number of parameters to a function in PostgreSQL</title>
		<link>http://sqlbrain.wordpress.com/2010/11/29/passing-a-variable-number-of-parameters-to-a-function-in-postgresql/</link>
		<comments>http://sqlbrain.wordpress.com/2010/11/29/passing-a-variable-number-of-parameters-to-a-function-in-postgresql/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 01:42:05 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=31</guid>
		<description><![CDATA[I had a situation where I wanted to pass a variable number of Key/Value pairs to an SQL function, which would then insert values into parent/child pages. After a bit of research, I found that Arrays were the ideal choice. Take a look at this code: CREATE OR REPLACE FUNCTION insert_stuff ( in_array TEXT[2][] ) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=31&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a situation where I wanted to pass a variable number of Key/Value pairs to an SQL function, which would then insert values into parent/child pages. After a bit of research, I found that Arrays were the ideal choice.</p>
<p>Take a look at this code:</p>
<pre>
CREATE OR REPLACE FUNCTION insert_stuff ( in_array TEXT[2][] ) RETURNS void AS $$
BEGIN
  -- Attribute records
  FOR i IN array_lower(in_array, 1)..array_upper(in_array, 1) LOOP
    INSERT INTO stuff (key, value)
      VALUES( in_array[i][1], in_array[i][2]);
  END LOOP;

END;
$$ LANGUAGE plpgsql;
</pre>
<p>The parameter being passed to the function is <code>in_array TEXT[2][]</code>, which means an array of 2-element Text arrays. Here&#8217;s some samples of calling the function:</p>
<pre>
select insert_stuff( ARRAY[['firstname','Fred']] );
select insert_stuff( ARRAY[['firstname','Fred'], ['food','Apple']] );
select insert_stuff( ARRAY[['country','Australia'], ['city','Sydney'], ['weather', 'Cloudy'] );
</pre>
<p>The result is the ability to pass any number of elements into the function. Yippee!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=31&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2010/11/29/passing-a-variable-number-of-parameters-to-a-function-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
		<item>
		<title>Using DISTINCT with CASE</title>
		<link>http://sqlbrain.wordpress.com/2010/09/23/using-distinct-with-case/</link>
		<comments>http://sqlbrain.wordpress.com/2010/09/23/using-distinct-with-case/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 02:25:30 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Clever Queries]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=27</guid>
		<description><![CDATA[I had an interesting situation where I wanted to count DISTINCT rows with variable values. Imagine a table like this: Customer City VIP Acme Sydney true Bally Sydney false Bally Melbourne true Costco Melbourne false David Jones Sydney true David Jones Melbourne true Embraer Sydney false Fastway Sydney true Now, how would you count the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=27&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had an interesting situation where I wanted to count DISTINCT rows with variable values.</p>
<p>Imagine a table like this:</p>
<table border="1">
<tbody>
<tr>
<th>Customer</th>
<th>City</th>
<th>VIP</th>
</tr>
<tr>
<td>Acme</td>
<td>Sydney</td>
<td>true</td>
</tr>
<tr>
<td>Bally</td>
<td>Sydney</td>
<td>false</td>
</tr>
<tr>
<td>Bally</td>
<td>Melbourne</td>
<td>true</td>
</tr>
<tr>
<td>Costco</td>
<td>Melbourne</td>
<td>false</td>
</tr>
<tr>
<td>David Jones</td>
<td>Sydney</td>
<td>true</td>
</tr>
<tr>
<td>David Jones</td>
<td>Melbourne</td>
<td>true</td>
</tr>
<tr>
<td>Embraer</td>
<td>Sydney</td>
<td>false</td>
</tr>
<tr>
<td>Fastway</td>
<td>Sydney</td>
<td>true</td>
</tr>
</tbody>
</table>
<p>Now, how would you count the number of distinct customers in each city by VIP status?</p>
<p>I&#8217;m looking for this result:</p>
<table border="1">
<tbody>
<tr>
<th>City</th>
<th>VIPs</th>
<th>Not VIPs</th>
</tr>
<tr>
<td>Melbourne</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>Sydney</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>
<p>You can&#8217;t just group by City and use COUNT(DISTINCT customer) because it has to be divided by VIP too. That&#8217;s when I realised that the DISTINCT keyword looks at the following expression rather than just a column name.</p>
<p>Here&#8217;s the code that can be used:</p>
<pre>select
  city,
  count(distinct case when     vip then customer else null end) as VIPs,
  count(distinct case when not vip then customer else null end) as Not_VIPs
from table
group by city</pre>
<p>The COUNT automatically ignores the null values, so it&#8217;s only count the number of distinct cities that are/are not VIPs.</p>
<p>That&#8217;s a very different way to use DISTINCT.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=27&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2010/09/23/using-distinct-with-case/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
		<item>
		<title>Concatenate rows with a PostgreSQL aggregate function</title>
		<link>http://sqlbrain.wordpress.com/2010/02/08/concatenate-rows-with-a-postgresql-aggregate-function/</link>
		<comments>http://sqlbrain.wordpress.com/2010/02/08/concatenate-rows-with-a-postgresql-aggregate-function/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 22:34:37 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=24</guid>
		<description><![CDATA[Aggregates are a nifty feature of PostgreSQL that let you write your own functions that operate over a number of rows, similar to MIN, MAX, COUNT and AVERAGE. Basically, a set of rows (typically generated via GROUP BY) are iterated through the Aggregate function, with a final call when the set of rows is complete. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=24&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Aggregates are a nifty feature of PostgreSQL that let you write your own functions that operate over a number of rows, similar to <code>MIN</code>, <code>MAX</code>, <code>COUNT</code> and <code>AVERAGE</code>.</p>
<p>Basically, a set of rows (typically generated via <code>GROUP BY</code>) are iterated through the Aggregate function, with a final call when the set of rows is complete.</p>
<ul>
<li>MIN works by keeping track of the smallest value passed in</li>
<li>COUNT works by incrementing a counter for each row</li>
<li>AVERAGE works like SUM and then divides by the equivalent to COUNT</li>
</ul>
<p>I had a need to concatenate strings from multiple rows into a single string, which I wanted to delimit by blank lines, commas or whatever. For example, let&#8217;s say I had some rows:</p>
<pre>NAME
----
John
David
Mary
Sally
</pre>
<p>I wanted to be able to call <code>smoosh(name, ', ')</code> and get the result: <code>John, David, Mary, Sally</code></p>
<p>The SMOOSH function below does just that:</p>
<pre>-- SMOOSH: Combines strings with supplied paramter
-- eg smoosh('fred', 'joe', ', ') ==&gt; 'fred, joe'
-- The 'strict' paramter automatically handles NULL inputs

CREATE FUNCTION smoosh(text, text, text) RETURNS text
  VOLATILE STRICT
AS $$$
DECLARE
  BEGIN
    IF  character_length($1) &gt; 0 THEN
      RETURN $1 || $3 || $2;
    ELSE
      RETURN $2;
    END IF;
  END;
$$$ LANGUAGE plpgsql;

-- Combine text with supplied delimiter
-- eg smoosh(name, ', ')
CREATE AGGREGATE smoosh(text, text)
(
  SFUNC = smoosh,
  STYPE = text
);

-- TEST HERE
/*
select smoosh(i::text, ', ')
FROM
  (select generate_series(1, 3) as i UNION select null as i UNION select 4 as i) foo
*/
</pre>
<p>Aggregates take a bit of brain power to interpret, but basically:</p>
<ul>
<li>The Smoosh FUNCTION concatenates two input strings, with a supplied delimiter in the middle</li>
<li>The Smoosh AGGREGATE just tells PostgreSQL to call the Smoosh FUNCTION for each row, and that it&#8217;s of type text</li>
</ul>
<p>Neat, eh?!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=24&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2010/02/08/concatenate-rows-with-a-postgresql-aggregate-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
		<item>
		<title>PostgreSQL concat operator doesn&#8217;t like NULLs</title>
		<link>http://sqlbrain.wordpress.com/2010/01/31/postgresql-concat-operator/</link>
		<comments>http://sqlbrain.wordpress.com/2010/01/31/postgresql-concat-operator/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 23:40:49 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Operators]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=21</guid>
		<description><![CDATA[I just found out that the &#124;&#124; concatenation operator returns NULL if one of the inputs is NULL. select '1' &#124;&#124; '2' ==&#62; null Fortunately, Berend Tober added a user comment to the PostgreSQL documentation with a solution&#8230; rewrite the &#124;&#124; operator! CREATE OR REPLACE FUNCTION public.textcat_null(text, text) RETURNS text AS $$$ SELECT textcat(COALESCE($1, ''), [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=21&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just found out that the || concatenation operator returns NULL if one of the inputs is NULL.</p>
<pre>
select '1' || '2'
==&gt; null
</pre>
<p>Fortunately, Berend Tober added a user comment to the <a href="http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html">PostgreSQL documentation</a> with a solution&#8230; rewrite the || operator!</p>
<pre>
CREATE OR REPLACE FUNCTION public.textcat_null(text, text)
  RETURNS text AS
$$$
SELECT textcat(COALESCE($1, ''), COALESCE($2, ''));
$$$
  LANGUAGE 'sql' VOLATILE;

CREATE OPERATOR ||(
  PROCEDURE = public.textcat_null,
  LEFTARG = text,
  RIGHTARG = text);

select '1' || '2'
==&gt; 12
</pre>
<p>Works like a charm, thanks Berend!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=21&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2010/01/31/postgresql-concat-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
		<item>
		<title>Comment your SQL queries to track down performance hogs!</title>
		<link>http://sqlbrain.wordpress.com/2009/12/03/comment-your-sql-queries/</link>
		<comments>http://sqlbrain.wordpress.com/2009/12/03/comment-your-sql-queries/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 04:16:34 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=18</guid>
		<description><![CDATA[Here&#8217;s a tip&#8230; Start your SQL queries with a comment that uniquely identifies the query or its source. For example, I put lots of SQL queries on wiki pages, so I include the Unique ID of the wiki page: -- Page 19323 select id from customer where ... Then if the SQL Query runs wild, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=18&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a tip&#8230;</p>
<p>Start your SQL queries with a comment that uniquely identifies the query or its source. For example, I put lots of <a href="http://confluence.atlassian.com/x/XgPvCw">SQL queries on wiki pages</a>, so I include the Unique ID of the wiki page:</p>
<pre>-- Page 19323
select id from customer
where ...</pre>
<p>Then if the SQL Query runs wild, I can check the commands running on my server and the Page ID is visible at the start of the query. I can then track down the bad SQL!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=18&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2009/12/03/comment-your-sql-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
		<item>
		<title>Avoid SQL Divide-by-zero errors by using NULLIF</title>
		<link>http://sqlbrain.wordpress.com/2009/10/06/avoid-sql-divide-by-zero-errors-by-using-nullif/</link>
		<comments>http://sqlbrain.wordpress.com/2009/10/06/avoid-sql-divide-by-zero-errors-by-using-nullif/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 04:24:24 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Clever Queries]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=13</guid>
		<description><![CDATA[I picked up this tip while reading SQL Hacks. Let&#8217;s say you&#8217;re receiving occasional divide by zero errors: select count(name) / count(*) as "Average" from xyz This will result in a division by error if there are no rows returned. It can be avoided by using the NULLIF function, which returns a null value if [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=13&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I picked up this tip while reading <a href="http://www.amazon.com/SQL-Hacks-Andrew-Cumming/dp/0596527993/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1254802269&amp;sr=8-1">SQL Hacks</a>.</p>
<p>Let&#8217;s say you&#8217;re receiving occasional divide by zero errors:</p>
<pre>select
  count(name) / count(*) as "Average"
from xyz
</pre>
<p>This will result in a <code>division by error</code> if there are no rows returned.</p>
<p>It can be avoided by using the <code>NULLIF</code> function, which returns a <code>null</code> value if the two passed-in expressions are equal:</p>
<pre>select
  count(name) / <span style="color:#ff0000;">NULLIF( count(*), 0)</span>
from xyz
</pre>
<p>So, if <code>count(*) = 0</code>, a <code>null</code> value is returned. The resulting division therefore equates to <code>null</code>. While that isn&#8217;t a particularly useful result, it&#8217;s better than getting an error!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=13&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2009/10/06/avoid-sql-divide-by-zero-errors-by-using-nullif/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
		<item>
		<title>Outer Joins are faster than INs</title>
		<link>http://sqlbrain.wordpress.com/2009/08/21/outer-joins-are-faster-than-ins/</link>
		<comments>http://sqlbrain.wordpress.com/2009/08/21/outer-joins-are-faster-than-ins/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 08:33:20 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Joins]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=8</guid>
		<description><![CDATA[I had a situation where I had to count Trial Licenses based on whether the person is an existing Customer. Think of it as two tables: license table customer table For each license, I had to lookup the customer table, with counts of how many matched and didn&#8217;t match. As a first pass, I did [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=8&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a situation where I had to count Trial Licenses based on whether the person is an existing Customer.</p>
<p>Think of it as two tables:</p>
<ul>
<li>license table</li>
<li>customer table</li>
</ul>
<p>For each license, I had to lookup the customer table, with counts of how many matched and didn&#8217;t match.</p>
<p>As a first pass, I did something like this:</p>
<pre>select
  to_char(created, 'YYYY-MM') as month,
  sum(case when owner in (select owner from customer) then 1 else 0 end) as "customer count",
  sum(case when owner in (select owner from customer) then 0 else 1 end) as "non-customer count",
  count(*) as "total"
from license</pre>
<p>Performance was bad because of the repeated calls to the customer table. I could have split it into two queries so that the sub-select went into the WHERE, but I&#8217;d have to stitch the results back together since I wanted a count of both customers and non-customers.</p>
<p>Then I tried it via a LEFT OUTER JOIN, something like this:</p>
<pre>
select
  to_char(created, 'YYYY-MM') as month,
  count(customers) as "customer count",
  count(*) - count(customers) as "non-customer count"
  count(*) as "total"
from license LEFT OUTER JOIN customer using owner</pre>
<p>Performance improved dramatically! A JOIN works so much faster than sub-selects, at least in PostgreSQL.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=8&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2009/08/21/outer-joins-are-faster-than-ins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
		<item>
		<title>Using logaritms for grouping large numbers</title>
		<link>http://sqlbrain.wordpress.com/2009/08/20/logarithms/</link>
		<comments>http://sqlbrain.wordpress.com/2009/08/20/logarithms/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 00:17:21 +0000</pubDate>
		<dc:creator>jrotenstein</dc:creator>
				<category><![CDATA[Clever Queries]]></category>

		<guid isPermaLink="false">http://sqlbrain.wordpress.com/?p=3</guid>
		<description><![CDATA[I had a bit of fun today. I had to show a summary of Revenue from some companies that I had downloaded from Jigsaw. (Jigsaw is a community site for gathering and sharing Company and Contact information. They&#8217;ve got a free API for accessing company information, too!) The revenue data was fairly random, like this: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=3&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a bit of fun today. I had to show a summary of Revenue from some companies that I had downloaded from <a href="http://www.jigsaw.com">Jigsaw</a>. (Jigsaw is a community site for gathering and sharing Company and Contact information. They&#8217;ve got a free API for accessing company information, too!)</p>
<p>The revenue data was fairly random, like this:</p>
<pre>
5000000
891900000
5000000
150000000
500000
30000000
5000000
1000000000
5000000
</pre>
<p>Even grouping the data wasn&#8217;t much better:</p>
<pre>
revenue       count
------------  -----
5000000       2543
500000        1636
30000000      1128
75000000      432
150000000     339
1000000000    338
350000000     252
750000000     156
1200000000    11
1100000000    10
1300000000    9
103630000000  9
2100000000    4
etc.
</pre>
<p>So, what would be the best way to present this information?</p>
<p>There&#8217;s too many possible values to group into units of $million. But perhaps grouping into orders of magnitude would work:</p>
<pre>select
  to_char(power(10, ceiling(log(revenue / 1000000))), 'FM$999,999,999"m"') as revenue,
  count(*)
from jigsaw
where
  revenue &gt; 1000000
group by ceiling(log(revenue / 1000000))
order by ceiling(log(revenue / 1000000))</pre>
<p>This results in:</p>
<pre>
revenue      count
-----------  -----
$1m          1
$10m         2552
$100m        1642
$1,000m      1287
$10,000m     234
$100,000m    184
$1,000,000m  24
</pre>
<p>Much nicer!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlbrain.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlbrain.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlbrain.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlbrain.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlbrain.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlbrain.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlbrain.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlbrain.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlbrain.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlbrain.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlbrain.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlbrain.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlbrain.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlbrain.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlbrain.wordpress.com&amp;blog=9085064&amp;post=3&amp;subd=sqlbrain&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlbrain.wordpress.com/2009/08/20/logarithms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/30589f4b94588d47883ea879f37d8ede?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jrotenstein</media:title>
		</media:content>
	</item>
	</channel>
</rss>
