Reputation: 11
I'm looking to return the most recent result where the letter_source is "CCMA", based on the date the letter_type is "P29".
Basically I need to know the date of the last "CCMA" letter only for accounts which have had a "P29" letter. The CCMA letter can be any date as long as it's the most recent, but the P29 must be within a specific time period.
So far my code is:
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;
Please could you help?
Upvotes: 1
Views: 167
Reputation: 4554
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;
Upvotes: 1
Reputation: 2016
I think you can do this using 2 perm letter tables. This isn't pretty but I'm not coming up with a more efficient way right now...
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
;
Upvotes: 0