简体   繁体   English

SQL - 只需要最大日期的记录

[英]SQL - Only need the record with Max Date

My table looks something like this:我的桌子看起来像这样:

在此处输入图像描述

I want to get the record for the MAX Date.我想获取 MAX 日期的记录。 So after querying, my output should only contain this:所以查询后,我的 output 应该只包含这个:

在此处输入图像描述

Use row_number() with top (1) with ties available for SQL Server (which was initially tagged):row_number()top (1) with ties使用,并带有可用于 SQL 服务器(最初被标记)的关系:

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

You can also use subquery:您还可以使用子查询:

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

A correlated subquery is a simple method:相关子查询是一种简单的方法:

select t.*
from t
where t.update_date = (select max(t2.update_date) from t t2 where t2.number = t.num);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM