Forked from lysender/select-top-n-for-each-group.sql
Created
September 28, 2021 09:32
-
-
Save Skrip42/d132f56b1fd8c6689fea97ce19c87cd0 to your computer and use it in GitHub Desktop.
MySQL Select top n for each group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set @num := 0, @type := ''; | |
select type, variety, price | |
from ( | |
select type, variety, price, | |
@num := if(@type = type, @num + 1, 1) as row_number, | |
@type := type as dummy | |
from fruits | |
order by type, price | |
) as x where x.row_number <= 2; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment