Comments on: SQL: ranking without self join, revisited https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited Blog by Shlomi Noach Sun, 30 Jan 2011 12:35:50 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: Kleyber Derick https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29210 Sun, 30 Jan 2011 12:35:50 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-29210 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>=’2011-01-04′
AND c.datalanc=’2011-01-04′
AND c.datalanc=’2011-01-04′
AND c.datalanc<='2011-02-02'
GROUP BY l.cod_loja
ORDER BY vlrloja DESC

]]>
By: Kleyber Derick https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29209 Sun, 30 Jan 2011 12:35:19 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-29209 Sorry, the SELECT is not complete. I am trying to put the real one, but I can’t.

]]>
By: Kleyber Derick https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29207 Sun, 30 Jan 2011 12:33:11 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-29207 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:

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>=’2011-01-04′
AND c.datalanc=’2011-01-04′
AND c.datalanc=’2011-01-04′
AND c.datalanc<='2011-02-02'
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.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-29173 Sun, 30 Jan 2011 06:38:48 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-29173 @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 SQL Pie Chart.

BTW, “WHERE c.ade = p.ade AND p.cod_loja = l.cod_loja” is redundant: you already use these conditions on the JOIN clauses.

]]>
By: Kleyber Derick https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-28881 Fri, 28 Jan 2011 00:45:34 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-28881 Hi Shlomi,
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:

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>=’2011-01-04′
AND c.datalanc<='2011-02-02'
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's blog too.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4336 Sun, 27 Sep 2009 07:11:10 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-4336 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’ve mostly repeated things that have been said there; I apparently offer little new.

Shlomi

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4283 Fri, 25 Sep 2009 13:22:41 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-4283 Hi Roland,

Thank you. I will make an effort to get more reliable information.
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.

Shlomi

]]>
By: Roland Bouman https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4280 Fri, 25 Sep 2009 11:21:54 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-4280 BTW: I just wanted to say I agree with your analysis regarding the limitions of using GROUP_CONCAT (as I did in my example)

]]>
By: Roland Bouman https://shlomi-noach.github.io/blog/mysql/sql-ranking-without-self-join-revisited/comment-page-1#comment-4279 Fri, 25 Sep 2009 11:20:43 +0000 https://shlomi-noach.github.io/blog/?p=1330#comment-4279 Hi Shlomi!

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.

Thanks so far 🙂

]]>