所以,我有这张桌子:
| COD | | ID | |BALANCE| | DATE |
20002978 34134987 2,1 2012-10-20 00:00:00.000
20002978 34134987 2,1 2012-10-30 00:00:00.000
20002978 34134987 10,1 2012-12-05 00:00:00.000
20002978 34134987 8,1 2012-12-22 00:00:00.000
20002978 34134987 9,1 2013-01-16 00:00:00.000
20002978 34134987 23,1 2013-01-19 00:00:00.000
20002978 34134987 7,1 2013-01-29 00:00:00.000
20002978 34134987 3,1 2013-02-02 00:00:00.000
80125573 34134987 13,1 2013-02-22 00:00:00.000
80125573 34134987 1,0 2013-03-08 00:00:00.000
我想选择最后一个代码的最后一个余额,其中ID是我将传递给过程的参数。
我需要这样的结果:
ID = 34134987
| COD | | ID | |BALANCE| | DATE |
20002978 34134987 3,1 2013-02-02 00:00:00.000
80125573 34134987 1,0 2013-03-08 00:00:00.000
任何提示?提前谢谢。
答案 0 :(得分:3)
试试这样......
Select Cod,Id,Balance,[Date] from (
Select Row_Number() Over(Partition By Cod,Id Order By [Date] desc) as Row, Cod,Id,Balance,Date
from table) t where t.Row=1