View previous topic :: View next topic
|
Author |
Message |
a69356
New User
Joined: 01 Dec 2006 Posts: 21 Location: Gurgaon
|
|
|
|
Hi,
suppose I have a table as mentioned below
Acct year month
1234 08 01
1235 08 10
1236 08 11
1237 07 01
1238 06 04
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.
Thanks, |
|
Back to top |
|
|
bhairon singh rathore
New User
Joined: 19 Jun 2008 Posts: 91 Location: banglore
|
|
|
|
Can you please let us know that year and month are two different column or one column as year which is a date |
|
Back to top |
|
|
a69356
New User
Joined: 01 Dec 2006 Posts: 21 Location: Gurgaon
|
|
|
|
Month and year are two different columns.
Thanks |
|
Back to top |
|
|
nabarundas
New User
Joined: 21 Jun 2007 Posts: 28 Location: pune
|
|
|
|
Try the following Query:
SELECT * FROM <TABLE> WHERE
TIMESTAMPDIFF (64, CHAR((CURRENT_TIMESTAMP)-
TIMESTAMP('20' || year || '-' || month ||'-01-00.00.00'))) > 12
Regards,
Nabarun |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
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)
and
year_column = year(Current date - 12 months)
)
OR
rows that are
(
mo_column <= month(Current date - 12 months)
and
year_column <= year(Current date - 12 months)
)
where mo_column and year_column are numeric datatype
? |
|
Back to top |
|
|
sajjan jindal Warnings : 1 New User
Joined: 09 Sep 2007 Posts: 60 Location: india
|
|
|
|
The current month and year is : 09 07
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.
select *
from account_details
where Year < 08
Union
select *
from account_details
where Year = 08
and Month < 07 |
|
Back to top |
|
|
Ketan Varhade
Active User
Joined: 29 Jun 2009 Posts: 197 Location: Mumbai
|
|
|
|
Hi ,
Select acct from Tab
where (:ws-curr-year - year > 1)
We can pass the current year to this variable 'ws-curr-year' and then check for the value
Correct me if I am wrong.
Regards
Ketan Varhade |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Did you test this before posting?
What happened? |
|
Back to top |
|
|
Ketan Varhade
Active User
Joined: 29 Jun 2009 Posts: 197 Location: Mumbai
|
|
|
|
Hi
Select acct from Tab
where (:ws-curr-year - year > 1)
or
(:ws-curr-year - year = 1) and (:ws-curr-month - month >= 0)
I haven't tested this but I think that this will work
Regards
Ketan |
|
Back to top |
|
|
|