I have the following table:
-----------------------------------------
xDate xItem xCount
-----------------------------------------
2018-01-01 A 100
2018-01-01 B 200
2018-01-01 D 500
2018-01-02 C 200
2018-01-02 E 800
I want to select TOP 2 value for each date on the MAX value of xCount field. So, the result should be:
-----------------------------------------
xDate xItem xCount
-----------------------------------------
2018-01-01 D 500
2018-01-01 B 200
2018-01-02 E 800
2018-01-02 C 200
Does anyone have an idea for this case?
Cheers,
You can try to use RANK
window function, if there more the two row have same xCount
then you want to get them all.
You can try to use dense_rank
instead of RANK
SELECT xDate,xItem,xCount
FROM (
SELECT *,RANK() OVER(PARTITION BY xDate ORDER BY xCount DESC) rn
FROM T
) t1
WHERE t1.rn <= 2
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