IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How can achieve this by SQl Query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
a69356

New User


Joined: 01 Dec 2006
Posts: 21
Location: Gurgaon

PostPosted: Fri Jul 03, 2009 11:55 am
Reply with quote

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
View user's profile Send private message
bhairon singh rathore

New User


Joined: 19 Jun 2008
Posts: 91
Location: banglore

PostPosted: Fri Jul 03, 2009 12:09 pm
Reply with quote

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
View user's profile Send private message
a69356

New User


Joined: 01 Dec 2006
Posts: 21
Location: Gurgaon

PostPosted: Fri Jul 03, 2009 12:18 pm
Reply with quote

Month and year are two different columns.

Thanks
Back to top
View user's profile Send private message
nabarundas

New User


Joined: 21 Jun 2007
Posts: 28
Location: pune

PostPosted: Fri Jul 03, 2009 4:29 pm
Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Jul 03, 2009 4:34 pm
Reply with quote

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
View user's profile Send private message
sajjan jindal
Warnings : 1

New User


Joined: 09 Sep 2007
Posts: 60
Location: india

PostPosted: Fri Jul 03, 2009 5:12 pm
Reply with quote

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
View user's profile Send private message
Ketan Varhade

Active User


Joined: 29 Jun 2009
Posts: 197
Location: Mumbai

PostPosted: Wed Jul 08, 2009 3:57 pm
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Jul 08, 2009 8:24 pm
Reply with quote

Hello,

Did you test this before posting?

What happened?
Back to top
View user's profile Send private message
Ketan Varhade

Active User


Joined: 29 Jun 2009
Posts: 197
Location: Mumbai

PostPosted: Thu Jul 09, 2009 4:38 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top