Hey guys,
I am trying to write a query that will return each employee's YTD balance. The problem is in the table, there are multiple YTD balances each one occuring in a specific year and month number. I want to only retrieve the most recent YTD balance. A sample of the table would look like this:
EMP # ANTY_YY ANTY_MM YTD BALANCE
001 2014 12 12,444.31
001 2015 01 13,444.31
001 2015 02 14,444.31
So basically I need to two MAX returns. First one to return the highest year which is 2015. Then one that will return the MAX ANTY_MM for that year and show the balance on that record.
I can get the first MAX to work and give me the latest year, but it shows me all the month records in that year. I can't get my query to show me the MAX month as well.
When I try this query, it gets me zero rows. Any idea what I'm doing wrong?? Thanks!
I am trying to write a query that will return each employee's YTD balance. The problem is in the table, there are multiple YTD balances each one occuring in a specific year and month number. I want to only retrieve the most recent YTD balance. A sample of the table would look like this:
EMP # ANTY_YY ANTY_MM YTD BALANCE
001 2014 12 12,444.31
001 2015 01 13,444.31
001 2015 02 14,444.31
So basically I need to two MAX returns. First one to return the highest year which is 2015. Then one that will return the MAX ANTY_MM for that year and show the balance on that record.
I can get the first MAX to work and give me the latest year, but it shows me all the month records in that year. I can't get my query to show me the MAX month as well.
Code:
select a.recip_ssn_nbr,
a.anty_yy,
a.anty_mm,
a.drop_ytd_amt
from
(SELECT a.RECIP_SSN_NBR,
a.ANTY_YY,
a.ANTY_MM,
a.DROP_YTD_AMT
FROM DSNP.PR01_T_DROP_AMTS a
WHERE a.RECIP_SSN_NBR = 111223333
and a.anty_yy = (select max(b.anty_yy) from dsnp.pr01_t_drop_amts b
where b.recip_ssn_nbr = a.recip_ssn_nbr) ) as A
where a.anty_mm = (select max(b.anty_mm) from dsnp.pr01_t_drop_amts b
where b.recip_ssn_nbr = a.recip_ssn_nbr)
When I try this query, it gets me zero rows. Any idea what I'm doing wrong?? Thanks!