<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: SQL: ranking without self join, revisited</title>
	<atom:link href="http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/feed" rel="self" type="application/rss+xml" />
	<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited</link>
	<description>Blog by Shlomi Noach</description>
	<lastBuildDate>Wed, 01 Feb 2012 20:47:51 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Kleyber Derick</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29210</link>
		<dc:creator>Kleyber Derick</dc:creator>
		<pubDate>Sun, 30 Jan 2011 12:35:50 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-29210</guid>
		<description>SELECT
    SUM(c.vlrbase) AS vlrloja,
    l.cod_loja,
    l.nome_loj,
    (SELECT
     SUM(c.vlrbase) AS vlrloja1
     FROM
         tab_comissao AS c, tab_prot AS p, tab_lojas AS l
     WHERE c.datalanc&gt;=&#039;2011-01-04&#039;
     AND c.datalanc=&#039;2011-01-04&#039;
     AND c.datalanc=&#039;2011-01-04&#039;
  AND c.datalanc&lt;=&#039;2011-02-02&#039;
GROUP BY l.cod_loja
ORDER BY vlrloja DESC</description>
		<content:encoded><![CDATA[<p>SELECT<br />
    SUM(c.vlrbase) AS vlrloja,<br />
    l.cod_loja,<br />
    l.nome_loj,<br />
    (SELECT<br />
     SUM(c.vlrbase) AS vlrloja1<br />
     FROM<br />
         tab_comissao AS c, tab_prot AS p, tab_lojas AS l<br />
     WHERE c.datalanc&gt;='2011-01-04'<br />
     AND c.datalanc='2011-01-04'<br />
     AND c.datalanc='2011-01-04'<br />
  AND c.datalanc&lt;=&#039;2011-02-02&#039;<br />
GROUP BY l.cod_loja<br />
ORDER BY vlrloja DESC</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kleyber Derick</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29209</link>
		<dc:creator>Kleyber Derick</dc:creator>
		<pubDate>Sun, 30 Jan 2011 12:35:19 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-29209</guid>
		<description>Sorry, the SELECT is not complete. I am trying to put the real one, but I can&#039;t.</description>
		<content:encoded><![CDATA[<p>Sorry, the SELECT is not complete. I am trying to put the real one, but I can't.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kleyber Derick</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29207</link>
		<dc:creator>Kleyber Derick</dc:creator>
		<pubDate>Sun, 30 Jan 2011 12:33:11 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-29207</guid>
		<description>Thank you fr replying Shlomi. I&#039;ve done some tests, but I really did not understand your proposal. Let me show you what I did:

SELECT
    SUM(c.vlrbase) AS vlrloja,
    l.cod_loja,
    l.nome_loj,
    (SELECT
     SUM(c.vlrbase) AS vlrloja1
     FROM
         tab_comissao AS c, tab_prot AS p, tab_lojas AS l
     WHERE c.datalanc&gt;=&#039;2011-01-04&#039;
     AND c.datalanc=&#039;2011-01-04&#039;
     AND c.datalanc=&#039;2011-01-04&#039;
  AND c.datalanc&lt;=&#039;2011-02-02&#039;
GROUP BY l.cod_loja
ORDER BY vlrloja DESC

This gives me the amount, the total amount and the percentage of each row... sorry for being a newbie in this case.</description>
		<content:encoded><![CDATA[<p>Thank you fr replying Shlomi. I've done some tests, but I really did not understand your proposal. Let me show you what I did:</p>
<p>SELECT<br />
    SUM(c.vlrbase) AS vlrloja,<br />
    l.cod_loja,<br />
    l.nome_loj,<br />
    (SELECT<br />
     SUM(c.vlrbase) AS vlrloja1<br />
     FROM<br />
         tab_comissao AS c, tab_prot AS p, tab_lojas AS l<br />
     WHERE c.datalanc&gt;='2011-01-04'<br />
     AND c.datalanc='2011-01-04'<br />
     AND c.datalanc='2011-01-04'<br />
  AND c.datalanc&lt;=&#039;2011-02-02&#039;<br />
GROUP BY l.cod_loja<br />
ORDER BY vlrloja DESC</p>
<p>This gives me the amount, the total amount and the percentage of each row... sorry for being a newbie in this case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29173</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Sun, 30 Jan 2011 06:38:48 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-29173</guid>
		<description>@Kleyber,
With GROUP BY this becomes significatnly more complicated. The easiest way out is to put all this in a subquery, then select all column, adding a ranking column to that. See first code samples on &lt;a href=&quot;http://code.openark.org/blog/mysql/sql-pie-chart&quot; rel=&quot;nofollow&quot;&gt;SQL Pie Chart&lt;/a&gt;.

BTW, &quot;WHERE c.ade = p.ade AND p.cod_loja = l.cod_loja&quot; is redundant: you already use these conditions on the JOIN clauses.</description>
		<content:encoded><![CDATA[<p>@Kleyber,<br />
With GROUP BY this becomes significatnly more complicated. The easiest way out is to put all this in a subquery, then select all column, adding a ranking column to that. See first code samples on <a href="http://code.openark.org/blog/mysql/sql-pie-chart" rel="nofollow">SQL Pie Chart</a>.</p>
<p>BTW, "WHERE c.ade = p.ade AND p.cod_loja = l.cod_loja" is redundant: you already use these conditions on the JOIN clauses.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kleyber Derick</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-28881</link>
		<dc:creator>Kleyber Derick</dc:creator>
		<pubDate>Fri, 28 Jan 2011 00:45:34 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-28881</guid>
		<description>Hi Shlomi,
I&#039;m quite new in MySQL but I have to create a ranking based on a specific SELECT using joined tables. My original select is this:

SELECT
l.cod_loja,
l.nome_loj,
SUM(c.vlrbase) AS soma
FROM
tab_comissao c INNER JOIN tab_prot p ON (c.ade=p.ade)
INNER JOIN tab_lojas l ON (l.cod_loja=p.cod_loja)
WHERE c.ade = p.ade AND p.cod_loja = l.cod_loja
AND c.datalanc&gt;=&#039;2011-01-04&#039;
AND c.datalanc&lt;=&#039;2011-02-02&#039;
GROUP BY l.cod_loja
ORDER BY soma DESC

Where gives me this result:

3 store-3 345874.54
7 store-7 213022.86
6 store-6 209934.94
...
...
...
57 store-57 2827.18

So, how could I do this in order to show me the correct ranking?

Thanks in advance,

Kleyber Derick 

PS: I put this question on Roland Bouman&#039;s blog too.</description>
		<content:encoded><![CDATA[<p>Hi Shlomi,<br />
I'm quite new in MySQL but I have to create a ranking based on a specific SELECT using joined tables. My original select is this:</p>
<p>SELECT<br />
l.cod_loja,<br />
l.nome_loj,<br />
SUM(c.vlrbase) AS soma<br />
FROM<br />
tab_comissao c INNER JOIN tab_prot p ON (c.ade=p.ade)<br />
INNER JOIN tab_lojas l ON (l.cod_loja=p.cod_loja)<br />
WHERE c.ade = p.ade AND p.cod_loja = l.cod_loja<br />
AND c.datalanc&gt;='2011-01-04'<br />
AND c.datalanc&lt;=&#039;2011-02-02&#039;<br />
GROUP BY l.cod_loja<br />
ORDER BY soma DESC</p>
<p>Where gives me this result:</p>
<p>3 store-3 345874.54<br />
7 store-7 213022.86<br />
6 store-6 209934.94<br />
...<br />
...<br />
...<br />
57 store-57 2827.18</p>
<p>So, how could I do this in order to show me the correct ranking?</p>
<p>Thanks in advance,</p>
<p>Kleyber Derick </p>
<p>PS: I put this question on Roland Bouman&#039;s blog too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4336</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Sun, 27 Sep 2009 07:11:10 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-4336</guid>
		<description>Yikes!!

Browsing through High Performance MySQL (2nd Ed.), I suddenly notice this has all been discussed at length, at the end of chapter 4.
Seems like I&#039;ve mostly repeated things that have been said there; I apparently offer little new.

Shlomi</description>
		<content:encoded><![CDATA[<p>Yikes!!</p>
<p>Browsing through High Performance MySQL (2nd Ed.), I suddenly notice this has all been discussed at length, at the end of chapter 4.<br />
Seems like I've mostly repeated things that have been said there; I apparently offer little new.</p>
<p>Shlomi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4283</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Fri, 25 Sep 2009 13:22:41 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-4283</guid>
		<description>Hi Roland,

Thank you. I will make an effort to get more reliable information.
I think we should avoid the situation where we say &quot;hmmm.. we&#039;re not sure about this so let&#039;s not use it&quot;, but push towards a reliable answer.

Shlomi</description>
		<content:encoded><![CDATA[<p>Hi Roland,</p>
<p>Thank you. I will make an effort to get more reliable information.<br />
I think we should avoid the situation where we say "hmmm.. we're not sure about this so let's not use it", but push towards a reliable answer.</p>
<p>Shlomi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4280</link>
		<dc:creator>Roland Bouman</dc:creator>
		<pubDate>Fri, 25 Sep 2009 11:21:54 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-4280</guid>
		<description>BTW: I just wanted to say I agree with your analysis regarding the limitions of using GROUP_CONCAT (as I did in my example)</description>
		<content:encoded><![CDATA[<p>BTW: I just wanted to say I agree with your analysis regarding the limitions of using GROUP_CONCAT (as I did in my example)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://code.openark.org/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4279</link>
		<dc:creator>Roland Bouman</dc:creator>
		<pubDate>Fri, 25 Sep 2009 11:20:43 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=1330#comment-4279</guid>
		<description>Hi Shlomi! 

Nice trick. I&#039;m not entirely convinced though, but it looks promising. I&#039;ll do my best to try and falsify the result (:p) not to bug you of course, but to achieve more certainty that this is indeed reliable. 

Thanks so far :)</description>
		<content:encoded><![CDATA[<p>Hi Shlomi! </p>
<p>Nice trick. I'm not entirely convinced though, but it looks promising. I'll do my best to try and falsify the result (:p) not to bug you of course, but to achieve more certainty that this is indeed reliable. </p>
<p>Thanks so far <img src='http://code.openark.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

