I have to write an cursor which will fetch account number which are 12 months older than the current month and year,
Initially what I thougth is I would subtract 1 from current year and get the account numbers. but this will fetch even those records also which are less than 12 month. e.g If I subtract 1 from current year i.e 09 it will be 08
and it will fetch all the rows for year 08 but in this year I don't need rows which have month 10 and 11 as these are not 12 month old.my question is how can achieve this by SQl Query?
Any help or any pointer will be highly appreciated.
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
is this to be a spufi or qmf query (batch) or is this imbedded sql in a program?
do you want
rows that are
mo_column = month(Current date - 12 months)
year_column = year(Current date - 12 months)
rows that are
mo_column <= month(Current date - 12 months)
year_column <= year(Current date - 12 months)
where mo_column and year_column are numeric datatype
So, if we take a difference of 12 months, we need not apply any check for records having Year less than 08.
For records with year 08 we will check the month and consider those records with month less than 07 (since current month is 07 - take a call on you want the records with 07 or not - difference of 12 months)
Theres no point looking at the records with year 09 since you are looking for records older than 12 months.
where Year < 08
where Year = 08
and Month < 07