Reputation: 85
How do I get the output like this in sql. I tried group by but it is not giving months, is there an easier way to do this.
select * from t1 order by 1,2 ;
id | month | amnt
----+-------+------
1 | feb | 50
1 | jan | 20
2 | apr | 30
2 | feb | 50
2 | jan | 30
2 | mar | 70
(6 rows)
Output
=========
id | month | amnt
----+-------+------
1 | feb | 50
2 | mar | 70
Upvotes: 0
Views: 103
Reputation: 81960
If you have window functions
Select Top 1 with ties *
From YourTable
Order By Row_Number() over (Partition By id order by amnt desc)
Returns
id month amnt
2 mar 70
1 feb 50
Upvotes: 1