I have this dataset:
shopID supplier supply_count
1 a 12
2 b 12
2 f 12
2 e 10
3 b 5
3 a 2
4 f 15
4 c 11
I have used this query to get 2nd highest value for each group:
select s1.shopId, max(s1.supply_count)
from supply s1
where supply_count NOT IN (
select max(supply_count)
from supply s2
where s1.shopId = s2.shopId
)
group by s1.shopId
The results I'm getting are:
shopID supply_count
2 10
3 2
4 11
The desired output is: (in case there is no second highest I want to present the highest)
shopID supply_count
1 12
2 10
3 2
4 11
Based on this question: Retrieve 2nd highest count by each group