I am trying to write a query for a large dataset with many joins and having trouble accomplishing a particular piece without some sort of subquery, which I am trying to avoid.
For an example table with columns ID, Size, Item there may be multiple records with the same ID. I want to return the record per ID which has the largest Size.
ID Size Item
1 5 a
1 10 b
2 3 c
2 6 d
2 11 e
3 2 f
Expected result
ID Size Item
1 10 b
2 11 e
3 2 f
I’ve tried various group and having approaches without success.
Using a subquery I can do it like this but for a large dataset I’d prefer not to do it this way
select id, size, item
from test
where size = (select max(size) from test t2 where id = test.id)
Any suggestions?
This should satisfy your requirements: For each id, return only the row with the largest size