使用SAS中的proc SQL返回最新值

时间:2015-07-09 15:39:48

标签: sql sas

我希望根据letter_type为“P29”的日期返回letter_source为“CCMA”的最新结果。

基本上我需要知道最后一封“CCMA”信的日期,仅适用于有“P29”字母的帐户。 CCMA信件可以是任何日期,只要它是最新的,但P29必须在特定时间段内。

到目前为止,我的代码是:

proc sql;
   select distinct
      account_no        as account_no
     ,letter_date   as letter_date
     ,letter_type   as letter_type
     ,source        as letter_source
   from uulster.perm_aura_letters
   where letter_type="P29"
     and letter_date >= '01jul2015'd and letter_date <= '09jul2015'd
   order by letter_date, letter_type;
quit;

请帮助你?

2 个答案:

答案 0 :(得分:1)

proc sql;
select * from (select distinct
        account_no      as account_no
        ,letter_date    as letter_date
        ,letter_type    as letter_type
        ,source         as letter_source
        from uulster.perm_aura_letters
        where letter_type="P29"
        and letter_source contains "CCMA"
        and letter_date >= '01jul2015'd and letter_date <= '09jul2015'd)
having letter_date=max(letter_date)
order by letter_date, letter_type
;
quit;

答案 1 :(得分:0)

我认为你可以使用2个烫发字母表来做到这一点。这不是很好,但我现在还没有提出更有效的方式...

select distinct
    account_no      as account_no
    ,letter_date    as letter_date
    ,letter_type    as letter_type
    ,source         as letter_source
from uulster.perm_aura_letters p29
join uulster.perm_aura_letters ccma on p29.account_no = ccma.account_no
where p29.letter_type="P29"
and ccma.letter_type = "CCMA"
and p29.letter_date >= '01jul2015'd and p29.letter_date <= '09jul2015'd
and ccma.letterdate = (subselect max date for account and ccma)
order by p29.letter_date, p.29.letter_type