質問

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?

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top