仅选择多行的最大值记录

时间:2016-05-25 09:10:09

标签: sql-server

我有一张表,其中包含

等记录
Unit    Rate    Date-effected
-------------------------------------------
ALHA    ILS     2014-03-02 00:00:00.000
ALHA    ILS     2014-08-02 00:00:00.000
BUCK    ILS     2013-02-14 00:00:00.000
BUCK    ILS     2014-03-02 00:00:00.000
BUCK    ILS     2014-08-02 00:00:00.000
CASC    ILD     2013-02-14 00:00:00.000
CASC    ILD     2014-03-02 00:00:00.000
CASC    ILD     2014-08-02 00:00:00.000   

现在,我只想在结果table中选择最大日期值记录。是,

Unit    Rate    DateEffected
-------------------------------------------
ALHA    ILS     2014-08-02 00:00:00.000
BUCK    ILS     2014-08-02 00:00:00.000
CASC    ILD     2014-08-02 00:00:00.000   

2 个答案:

答案 0 :(得分:2)

希望这有帮助。

0 path

答案 1 :(得分:2)

您可以使用ROW_NUMBER

SELECT Unit, Rate, DateEffected
FROM (
  SELECT Unit, Rate, DateEffected,
         ROW_NUMBER() OVER (PARTITION BY Unit 
                            ORDER BY DateEffected DESC) AS rn
FROM mytable) AS t
WHERE t.rn = 1