<?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>Full of BS &#187; SQL</title>
	<atom:link href="http://fullof.bs/category/programming/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://fullof.bs</link>
	<description>He just never stops talking</description>
	<lastBuildDate>Thu, 15 Dec 2011 20:00:48 +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>A better approach to MySQL row generation</title>
		<link>http://fullof.bs/a-better-approach-to-mysql-row-generation/</link>
		<comments>http://fullof.bs/a-better-approach-to-mysql-row-generation/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 19:01:21 +0000</pubDate>
		<dc:creator>John Haugeland</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://fullof.bs/?p=612</guid>
		<description><![CDATA[I love Markus Winand.  Use the Index, Luke taught me a lot about SQL. Markus has produced a coping strategy for some of MySQL&#8217;s limitations in generating sequences.  I present an alternative form which produces arbitrary sized ranges, which he seems to believe is not possible.  Ah, Markus: it is. So, this article is actually [...]]]></description>
			<content:encoded><![CDATA[<p>I love Markus Winand.  <em><strong>Use the Index, Luke</strong></em> taught me a lot about SQL.</p>
<p><del>Markus has <a title="Use the Index Luke's MySQL row generator" href="http://use-the-index-luke.com/blog/2011-07-30/mysql-row-generator">produced a coping strategy for some of MySQL&#8217;s limitations in generating sequences</a>.  I present an alternative form which produces arbitrary sized ranges, which he seems to believe is not possible.  Ah, Markus: it is.</del></p>
<p>So, this article is actually wrong.  There&#8217;s a call-time limitation I didn&#8217;t know about: apparently in MySQL you can&#8217;t call a stored procedure from a query.  That renders this seriously useless.  I&#8217;m leaving the article up just for the sake of permanence.</p>
<h1 style="padding-left: 30px;"><strong><em><span style="color: #339966;">This advice is wrong.  Do not believe it.  I was in error.  It is kept only for posterity.</span></em></strong></h1>
<pre>-- -------
--
--  in re: http://use-the-index-luke.com/blog/2011-07-30/mysql-row-generator
--
--  No, this way is not our only hope: there is another.

drop procedure if exists nseq;

delimiter //

-- -------
--
--  Use union all to generate a 2-row table.  
--  Use log 2 to find power expansion count to cap the desired size.
--  Use cross joins to expand our 2-row to the smallest superior table.
--  Select the diminished part to get our sequence.

create procedure nseq(in upto integer)
begin

    declare itLeft integer default -1;

    set itLeft = ceil(log2(upto));

    set @front := 'select rownum from (select @rownum := @rownum+1 as rownum
                   from (SELECT @rownum := 0) r, (select 1 as hidden union
                   all select 2) a0';
    set @upTo  := upto;

    set @interior := '';
    while (itLeft &gt; 1) do
        set @interior := concat(@interior, ' cross join (select 1 as hidden
                         union all select 2) a', cast(itLeft as char));
        set itLeft    := itLeft - 1;
    end while;

    set @back  := ') sl where rownum &lt;= ?;';
    set @query := concat(@front, @interior, @back);

    prepare stmt from @query;
    execute stmt using @upTo;  
    deallocate prepare stmt;  

end //

delimiter ;</pre>
<p>Hopefully he will agree, or explain what I didn&#8217;t understand in his needs.  <img src='http://fullof.bs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://fullof.bs/a-better-approach-to-mysql-row-generation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implementing Geometric Mean in MySQL</title>
		<link>http://fullof.bs/implementing-geometric-mean-in-mysql/</link>
		<comments>http://fullof.bs/implementing-geometric-mean-in-mysql/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 19:06:09 +0000</pubDate>
		<dc:creator>John Haugeland</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://fullof.bs/?p=206</guid>
		<description><![CDATA[This turns out to be pretty easy.  You might also want to read Implementing Harmonic Mean in MySQL. Why bother blogging something this simple?  Because I couldn&#8217;t find one pre-made, and that means it&#8217;s time to bring my mighty google rank of like negative two to bear to fix the problem. CREATE TABLE example(val integer); [...]]]></description>
			<content:encoded><![CDATA[<p>This turns out to be pretty easy.  You might also want to read <a title="Harmonic Mean" href="http://fullof.bs/implementing-harmonic-mean-in-mysql">Implementing Harmonic Mean in MySQL</a>.</p>
<p>Why bother blogging something this simple?  Because I couldn&#8217;t find one pre-made, and that means it&#8217;s time to bring my mighty google rank of like negative two to bear to fix the problem.</p>
<pre style="padding-left: 30px">CREATE TABLE example(val integer);
INSERT INTO example VALUES(1),(2),(4),(8),(16);
SELECT exp(avg(ln(val))) as gmean from example;</pre>
<p>That&#8217;s it.  You should see output like this:</p>
<pre style="padding-left: 30px"><strong>mysql&gt; CREATE TABLE example(val integer);</strong>
Query OK, 0 rows affected (0.09 sec)

<strong>mysql&gt; INSERT INTO example VALUES(1),(2),(4),(8),(16);</strong>
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

<strong>mysql&gt; SELECT exp(avg(ln(val))) as gmean from example;</strong>
+- - - -+
| gmean |
+- - - -+
|     4 |
+- - - -+
1 row in set (0.00 sec)
</pre>
<p>Still haven&#8217;t figured out how to do central moments, skewness or kurtosis.</p>
]]></content:encoded>
			<wfw:commentRss>http://fullof.bs/implementing-geometric-mean-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implementing Harmonic Mean in MySQL</title>
		<link>http://fullof.bs/implementing-harmonic-mean-in-mysql/</link>
		<comments>http://fullof.bs/implementing-harmonic-mean-in-mysql/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 19:02:14 +0000</pubDate>
		<dc:creator>John Haugeland</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://fullof.bs/?p=207</guid>
		<description><![CDATA[This turns out to be pretty easy.  You might also want to read Implementing Geometric Mean in MySQL. Why bother blogging something this simple?  Because I couldn&#8217;t find one pre-made, and that means it&#8217;s time to bring my mighty google rank of like negative two to bear to fix the problem. CREATE TABLE example(val integer); [...]]]></description>
			<content:encoded><![CDATA[<p>This turns out to be pretty easy.  You might also want to read <a title="Geometric Mean" href="http://fullof.bs/implementing-geometric-mean-in-mysql">Implementing Geometric Mean in MySQL</a>.</p>
<p>Why bother blogging something this simple?  Because I couldn&#8217;t find one pre-made, and that means it&#8217;s time to bring my mighty google rank of like negative two to bear to fix the problem.</p>
<pre style="padding-left: 30px">CREATE TABLE example(val integer);
INSERT INTO example VALUES(1),(2),(4),(8),(16);
SELECT count(val) / sum(1/val) as hmean from example;</pre>
<p>That&#8217;s it.  You should see output like this:</p>
<pre style="padding-left: 30px"><strong>mysql&gt; CREATE TABLE example(val integer);</strong>
Query OK, 0 rows affected (0.09 sec)

<strong>mysql&gt; INSERT INTO example VALUES(1),(2),(4),(8),(16);</strong>
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

<strong>mysql&gt; SELECT count(val)/sum(1/val) as hmean from example;
</strong>+- - - - +
| hmean  |
+- - - - +
| 2.5806 |
+- - - - +
1 row in set (0.00 sec)</pre>
<p>Still haven’t figured out how to do central moments, skewness or kurtosis.</p>
]]></content:encoded>
			<wfw:commentRss>http://fullof.bs/implementing-harmonic-mean-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

