0

I have table like followed by

ID Cust_ID Amount
1   1       10
2   1       20
3   2       30
4   2       40
5   3       50

and I need the latest record of Cust_Id result like followed by

ID  Cust_Id Amount
2    1       20
4    2       40
5    3       50

And I tried the Query like this

Select Top 1 *Id,Cust_Id,Amount from tablename
Group by Cust_Id
Order by Id desc
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828

3 Answers3

1

You can try using row_number()

select * from
(
select *,row_number() over(partition by cust_id order id desc) rn from tablename
)A where rn=1
Fahmi
  • 37,315
  • 5
  • 22
  • 31
0

use row_number()

select * from     
(
select *,
row_number() over(partition by cust_id order by Amount desc) rn   
from tabl
) t where rn=1

or use corelate subquery

 select t1.* from table t1
  where t1.Amount= (select max(Amount) from table t2 
                     where t1.cust_id=t2.cust_id)
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

You can also try like following

select id,cust_id,amount
from YourTable t1
where id =(select max(t2.id) from YourTable  t2 where t2.Cust_ID=t1.Cust_ID)
order by id
PSK
  • 17,547
  • 5
  • 32
  • 43