MSSQL如何获取最新金额

时间:2019-03-11 06:43:27

标签: sql sql-server

如何获取最新金额,我已经有一些查询,但是显示了两条记录,我希望仅显示“ 7370”当前金额

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以使用相关子查询

select * from tablename a where lasttime in (select max(lasttime) from tablename b where a.id=b.id)

或者您可以使用row_number()

select * from
(
select *,row_number() over(partition by id order by lasttime desc) as rn from tablename
)A where rn=1

答案 1 :(得分:0)

只需在字段前面添加前1名。

Select TOP 1 fields from table

答案 2 :(得分:0)

  SELECT TOP 1 currentBalance FROM DBO.tbl_billing ORDER BY [date]