简体   繁体   中英

SQL - Only need the record with Max Date

My table looks something like this:

在此处输入图像描述

I want to get the record for the MAX Date. So after querying, my output should only contain this:

在此处输入图像描述

Use row_number() with top (1) with ties available for SQL Server (which was initially tagged):

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);

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.

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