Skip to content
Advertisement

Select rows where field is maximum, relative to another field

I have data in the following format:

ID – Value
a – 1
a – 2
a – 3
a – 4
b – 1
b – 2
b – 3
c – 1
c – 2
d – 4

And I would like to select rows with the maximum value for each ID, so that the output would be:

ID Value
a – 4
b – 3
c – 2
d – 4

I have tried conditioning on max(value), but am having trouble making this relative to the ID

Advertisement

Answer

If I understood that correctly, what you are trying to do is get the Max value per ID. If that is your intention, the query below should work fine:

select max(value) from myTable group by ID;

Hope this helps.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement