Jitesh
Jitesh

Reputation: 345

Get the last row from every year using mysql query

I need to get the row where the due_date field has the last month in every year. For eg: if I have 3 entries with due_date field like 2014-5-21,2014-6-21,2014-7-21 I need the last row in year 2014, that will be 2014-7-21, like wise in 2015 and the following years. Can someone help me out with this. I tried but nothing worked out

SELECT distinct(year(due_date)) FROM `vw_mortgage_repayment_schedule_org` 
where mortgage_id ='AREM-1408614735-VLASFAQ8VI'
and month(due_date) = max(month())

I need all the last rows for the given mortgage of every year eg- 2014,2015,2016 etc

Upvotes: 0

Views: 22

Answers (1)

Paul Stanley
Paul Stanley

Reputation: 4098

I think if you group by the year of the due_date, that might just about give you what you need, given that we search for the max month in the select, and group by the year. Possibly. Can we have your table structure?

SELECT year(due_date), month(max(due_date)), max(due_date)
FROM `vw_mortgage_repayment_schedule_org` 
where mortgage_id ='AREM-1408614735-VLASFAQ8VI'
GROUP BY year(due_date) 

Upvotes: 1

Related Questions