How to get the second highest value per group?
1
vote
2
answers
4685
views
Getting the second highest value from a table has been solved many times, but I'm looking for the second highest value **in each group**.
Given this table:
+----+-----+
| A | 10 |
| A | 20 |
| A | 35 | <-- This record
| A | 42 |
| B | 12 |
| B | 21 | <-- This record
| B | 33 |
| C | 14 |
| C | 23 |
| C | 38 |
| C | 41 | <-- This record
| C | 55 |
+----+-----+
I'd like to get the marked rows.
Pseudo-code:
select col_a, penultimate(col_b)
from foo
group by col_a;
Asked by RonJohn
(694 rep)
Nov 30, 2021, 02:56 PM
Last activity: Dec 1, 2021, 04:21 PM
Last activity: Dec 1, 2021, 04:21 PM