Select 基于日期列的最新数据

Select latest data based on date column

我有一个 table ACCT,

COMPANY    ACCOUNT    EFFDT        NAME
25         1001       2015-12-01   ABC Ltd.
25         1001       2012-01-01   ABC Inc
30         1001       2012-01-01   ABC Inc
10         1001       2012-01-01   ABC Inc
10         3800       2011-05-01   RAC Corp
25         3800       2011-05-01   RAC Corp
30         3800       2011-05-01   RAC Corp

我正在尝试根据帐户的 EFFDT 列获取一组帐户的最后一条记录 #

我要找的结果是,

| 25  | 1001  | 2015-12-01   | ABC Ltd.
| 30  | 3800  | 2011-05-01   | RAC Corp

我试过使用,

select * 
  from acct t1 
    inner join (
        select account as a, max(effdt) as b, max(company) as c 
          from acct 
          where account in (1001, 3800) 
          group by account
        ) t2 
      on t1.account = t2.a 
        and t1.effdt = t2.b 
        and t1.company = t2.c;

但我只得到,

| 30  | 3800  | 2011-05-01   | RAC Corp

使用ROW_NUMBER

https://chartio.com/resources/tutorials/how-to-use-row_number-in-db2/

  SELECT
      company,
      account,
      effdt,
      name
    FROM (SELECT
      company,
      account,
      effdt,
      name,
      ROW_NUMBER() OVER (PARTITION BY account ORDER BY effdt DESC, company DESC) rn  
    FROM acct) x
    WHERE x.rn = 1

这应该有效:

select max(a.company), a.account, a.effdt, a.name from acct a
join(select account, max(EFFDT) as m  
    from acct 
    group by account)
b on a.account = b.account and a.EFFDT = m  
group by a.account, a.effdt, a.name