簡體   English   中英

SQL - 只需要最大日期的記錄

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

我的桌子看起來像這樣:

在此處輸入圖像描述

我想獲取 MAX 日期的記錄。 所以查詢后,我的 output 應該只包含這個:

在此處輸入圖像描述

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

您還可以使用子查詢:

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

相關子查詢是一種簡單的方法:

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