Replace any occurrence of "date('200901')" by
Code:
timestamp_format('200901', 'YYYYMM')
Actually, this function returns a TIMESTAMP datatype, but since you pass its result to either the YEAR() or the MONTH() function that should work as well.
If not, pass it as argument to the DATE() function first.
Actually, a more direct way to obtain month and year from the subtraction of two months from a 'YYYYMM' expression is with two CASE expressions:
Code:
SELECT CASE substr('200901',5)
WHEN '01' THEN '11' WHEN '02' THEN '12' WHEN '03' THEN '01' ... END
AS month, ....
FROM ...
and
Code:
SELECT CASE WHEN substr('200901',5) >= '03'
THEN SUBSTR('200901',1,4)
ELSE char(int(SUBSTR('200901',1,4))-1) END AS year, ....
FROM ...