I have a table like this:
ID DateValue Amount
-----------------------------
1 2017/08/09 217
1 2017/08/08 204
1 2017/08/07 322
2 2017/08/09 1543
2 2017/08/08 1285
3 2017/08/09 12
4 2017/08/09 347
I only want to obtain the first rows for the most recent "DateValue" value. Like this:
ID DateValue Amount
-----------------------------
1 2017/08/09 217
2 2017/08/09 1543
3 2017/08/09 12
4 2017/08/09 347
I thought of using the "MAX" method like this:
SELECT ID, MAX(DateValue), MAX(Amount) FROM [dbo].[MyTable] GROUP BY Id
But for the ID 1, I get 322 while I should have 217 (the most recent value)
Is there a simple way to accomplish what I need?
This also work ,
select ID,DateValue,Amount from
(select ID,DateValue,Amount,ROW_NUMBER() over
(partition by ID order by DateValue Desc) RowNumber
from [dbo].[MyTable]) x
where RowNumber=1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.