I have a table that looks like this:
col1 col2 col3
------ ----- -----
A 1 trout
A 2 trout
B 1 bass
C 1 carp
C 2 tuna
D 1 salmon
I really only want to select the rows with the max value for col2.
The query I want to generate would return the following:
col1 col2 col3
------ ----- -----
A 2 trout
B 1 bass
C 2 tuna
D 1 salmon
I’ve tried something like this:
select col1, max (col2) as mCol2, col3
from mytable
group by col1, col2
In this case I get:
col1 Mcol2 col3
------ ----- -----
A 2 trout
B 1 bass
C 1 carp
C 2 tuna
D 1 salmon
As you can see, I still get C, 1, carp, when I’m only wanting C, 2, tuna.
I’ve considered trying to do something like
select col1, col2, col3
from mytable
where
col1-n-col2 in (
select col1, max (col2) as mCol2
from mytable)
group by col1, col2
But I don’t think that’s legal in SQL. What obvious solution have I missed?
you were close