Using the following sqlfiddle here How would I find the most recent payment made between the months of 2012-04-1 and 2012-03-31 using the case statement as in the previous queries
I tried this:
max(case when py.pay_date >= STR_TO_DATE(CONCAT(2012, '-04-01'),'%Y-%m-%d') and py.pay_date <= STR_TO_DATE(CONCAT(2012, '-03-31'), '%Y-%m-%d') + interval 1 year then py.amount end) CURRENT_PAY
However the answer I am getting is incorrect, where the actual answer should be:(12, '2012-12-12', 20, 1)
Please Provide me with some assistance, thank you.
Rather than a
CASEinside yourMAX()aggregate, that condition belongs in theWHEREclause. This joins against a subquery which pulls the most recent payment perperson_idby joining onMAX(pay_date), person_id.Here is an updated fiddle with the ids corrected in your table (since a bunch of them were 15). This returns record 18, for
2013-03-28.Update
After seeing the correct SQL fiddle… To incorporate the results of this query into your existing one, you can
LEFT JOINagainst it as a subquery onp.id.