Skip to content

Instantly share code, notes, and snippets.

@felclef
Created May 16, 2012 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felclef/2713550 to your computer and use it in GitHub Desktop.
Save felclef/2713550 to your computer and use it in GitHub Desktop.
Simulating an partition-by-ordered-count (lol) in MySQL
-- olc: select id_quadro, user_hash from ( select iq.id_quadro, user_hash, if( @prev <> iq.id_quadro, @rownum := 1, @rownum := @rownum+1 ) as g_rank, @prev := iq.id_quadro from r7_inscricoes_quadros iq order by id_quadro, data_cadastro ) sq where g_rank <= 4
-- the sample uses some database wich I have not luvd... yet...
select
id_quadro,
user_hash
from
(
select
iq.id_quadro,
user_hash,
if( @prev <> iq.id_quadro, @rownum := 1, @rownum := @rownum+1 ) as g_rank,
@prev := iq.id_quadro
from
r7_inscricoes_quadros iq
order by
id_quadro, data_cadastro
) sq
where
g_rank <= 4;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment