Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

How can achieve this by SQl Query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: How can achieve this by SQl Query
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    Post subject: Reply to: SQL problem
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    Post subject:
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    Post subject: Reply to: How can achieve this by SQl Query
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: 6968
Location: porcelain throne

PostPosted: Fri Jul 03, 2009 4:34 pm    Post subject:
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    Post subject:
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    Post subject:
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

Site Director


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

PostPosted: Wed Jul 08, 2009 8:24 pm    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm
No new posts Omegamon for DB2 query ashek15 DB2 0 Wed Feb 15, 2017 11:25 am
No new posts Query on DB2 WLM stored procedures ashek15 DB2 8 Sun Feb 12, 2017 11:13 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us