MySQL Forums
Forum List  »  General

Current Month vs Last Month in Jan
Posted by: L B
Date: August 13, 2015 04:54PM

I’m trying to do a last month vs current month comparison (disregarding what day of the month it is and it does not need to be extrapolated for the full month). I came up with:

SELECT MONTH(X.date_created)
FROM example_table X
WHERE X.date_created >= date_sub(curdate(), INTERVAL 63 DAY)
GROUP BY MONTH(X.date_created)

But if you pull this at the beginning of a month, it will bring back some of ‘2 months ago’ stats (because I’m using the 63 day interval). So I came up with this:

SELECT MONTH(X.date_created)
FROM lead_tally X
WHERE X.date_created >= date_sub(curdate(), INTERVAL 63 DAY)
AND MONTH(X.date_created) > MONTH(curdate())-2
AND MONTH(X.date_created) < MONTH(curdate())+1
GROUP BY
MONTH(X.date_created)

This will ONLY bring back the current month and last month, regardless of the interval. However, when this runs on Jan 1, it won’t work!

Any ideas on how to write a better Where to account for the first month of the year to be compared with Dec?

Options: ReplyQuote


Subject
Written By
Posted
Current Month vs Last Month in Jan
L B
August 13, 2015 04:54PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.