Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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