Question

I have a table like this

Ids Part Values
1   1    value1
1   2    value2
2   1    value3
3   1    some other value
4   1    something1
4   2    something else

I would like to group by Ids and get values for max(part).

I tried below query -

select * from Parts group by Ids having Part = max(Part)

But this query is not always returning the values I want.

I want the result as some thing like

Ids Part Values
1   2     value2
2   1     value3
3   1     some other value
4   2     something else

Can you please help me with the query?

Was it helpful?

Solution

Try this you need to use self join and get the maxima from same table to join

select t1.* from Table1 t1
join 
(SELECT MAX(Part) Part ,Ids from Table1 GROUP BY Ids ) t2
on(t1.Ids=t2.Ids and t1.part =t2.Part)

See fiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top