首页 > 解决方案 > SQL - 只需要最大日期的记录

问题描述

我的桌子看起来像这样:

在此处输入图像描述

我想获取 MAX 日期的记录。所以查询后,我的输出应该只包含这个:

在此处输入图像描述

标签: sql

解决方案


使用row_number()with top (1) with tiesavailable for SQL Server(最初被标记):

select top (1) with ties t.*
from table t
order by row_number() over (partition by no order by date desc);

您还可以使用子查询:

select t.*
from (select t.*, row_number() over (partition by no order by date desc) as seq
      from table t
     ) t
where seq = 1;

推荐阅读