I have a table like this:
Rank Letter 1 A 2 A 3 B 4 A 5 C 6 A 7 C 8 C 9 B 10 C
And I need the top 2 of each letter ordered by ascending rank:
Rank Letter 1 A 2 A 3 B 5 C 7 C 9 B
How would I do it? It's fairly straightforward to get just the top 1 using GROUP BY, but I can't seem to get it working for multiple entries
SELECT mo.Letter, md.Rank
FROM (
SELECT DISTINCT letter
FROM mytable
) mo
JOIN mytable md
ON md.Letter >= mo.Letter
AND md.Letter <= mo.Letter
AND Rank <=
COALESCE
(
(
SELECT Rank
FROM mytable mi
WHERE mi.letter = mo.letter
ORDER BY
Rank
LIMIT 1, 1
),
0xFFFFFFFF
)
You need to have a composite index on (Letter, Rank)
(in this order)
Note this construct:
md.Letter >= mo.Letter
AND md.Letter <= mo.Letter
instead of mere md.Letter = mo.Letter
It forces Range checked for each record
which is more efficient.
See this article in my blog:
for more details on this.
select distinct rank, letter
from table1 t2
where rank in
(select top 2 rank
from table1 t2
where t2.letter = t1.letter
order by rank)
order by letter, rank
EDIT: (my first try won't work on MySql (Quassnoi comment), I modified it to work on sql server for example)
second try:
select t.letter, t.rank
from table1 t
join (
select t1.letter, min(t1.rank) m
from table1 t1
join (select t0.letter, min(t0.rank) m, count(1) c
from table1 t0 group by t0.letter) t2
on t1.letter = t2.letter and ((t2.c = 1) or (t2.c > 1 and t1.rank > m))
group by t1.letter) t3
on t.letter = t3.letter and t.rank <= t3.m
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With