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

Query changes from weekly report to monthly report


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

New User


Joined: 15 Jun 2007
Posts: 92
Location: bangalore

PostPosted: Fri Nov 16, 2007 7:07 pm
Reply with quote

Hi, I am having issue with DB2 Query.

Currently my job is running weekly and generating report for a week. The existing query is like this

SELECT aaa_timestamp,bbb,ccc FROM xyz WHERE DATE (aaa_timestamp) BETWEEN (CURRENT DATE-6 DAYS) AND (CURRENT DATE)

Now i have to modify to monthly job and i have to generate report for the last entire month.
Since the query has been hard coded directly inside a jcl. So we have to specify all the conditions in a single query.

[Note: We have to take care that every month having different days and also leap year]

could please any one suggest for this, its urgent.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Nov 16, 2007 7:33 pm
Reply with quote

chandracdac wrote:
Hi, I am having issue with DB2 Query.

Currently my job is running weekly and generating report for a week. The existing query is like this

SELECT aaa_timestamp,bbb,ccc FROM xyz WHERE DATE (aaa_timestamp) BETWEEN (CURRENT DATE-6 DAYS) AND (CURRENT DATE)

Now i have to modify to monthly job and i have to generate report for the last entire month.
Since the query has been hard coded directly inside a jcl. So we have to specify all the conditions in a single query.

[Note: We have to take care that every month having different days and also leap year]

could please any one suggest for this, its urgent.

Code:
WHERE DATE (aaa_timestamp) BETWEEN  (CURRENT DATE- DAY(CURRENT DATE) DAYS + 1 DAYS) AND  (CURRENT DATE)

WARNING: If the query is not run on the last day of the month (ie. your system is down and you don't get to run it until the next day) you will get incorrect results.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Fri Nov 16, 2007 7:50 pm
Reply with quote

Quote:
SELECT aaa_timestamp,bbb,ccc FROM xyz WHERE DATE (aaa_timestamp) BETWEEN (CURRENT DATE-6 DAYS) AND (CURRENT DATE)


the query does NOT satisfy correctly the Business objectives for weekly/monthly reporting

a better way of approaching the business requirement for a weekly report
would be

Quote:
SELECT needed_columns FROM table
WHERE date BETWEEN first_date_of_the_period AND last_date_of_the_period


where beforehand the first date and the last date have been determined
thru a business calendar,

this approach will have the advantage of making the query independent from the run day
- quite advisable with automated scheduling,
if the query has to run after a batch process running in the evening,
a delay in the predecessors will cause Your query to run the following day
giving inconsistent results

/rant on
something that still amazes me after many year of IT consulting
is how hard is to make organizations understand and act in the right way
on calendar related issues
/rant off
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Nov 16, 2007 8:44 pm
Reply with quote

1. They are switching from weekly to monthly. The query the op posted would get the last weeks worth of data, not necessarily a calendar week.

2. I missed the "last complete month" part so to correct that use:
Code:
WHERE DATE (aaa_timestamp) BETWEEN  (CURRENT DATE - DAY (CURRENT DATE) DAYS  - DAY((CURRENT DATE) - DAY(CURRENT DATE)DAYS) DAYS
    + 1 DAYS) AND  (CURRENT DATE - DAY(CURRENT DATE) DAYS)

Messy but it works.
Back to top
View user's profile Send private message
senthilkuduva

New User


Joined: 16 Nov 2007
Posts: 2
Location: chennai

PostPosted: Sat Nov 17, 2007 8:36 am
Reply with quote

Hi, I am also working on the same problem, Here we cant simply mention like between first day and last day of month because,

If it is running on jan 1st then has to generate report for dec which is having31 days

If it is running on mar 1st then has to generate report for feb which is having 28 days and for leap year 29 days


If it is running on May 1st then has to generate report for APr which is having30 days

Please suggest query for this.
Back to top
View user's profile Send private message
chandracdac

New User


Joined: 15 Jun 2007
Posts: 92
Location: bangalore

PostPosted: Sat Nov 17, 2007 9:16 am
Reply with quote

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

New User


Joined: 15 Jun 2007
Posts: 92
Location: bangalore

PostPosted: Sat Nov 17, 2007 9:23 am
Reply with quote

Thanks craq its working..
Back to top
View user's profile Send private message
senthilkuduva

New User


Joined: 16 Nov 2007
Posts: 2
Location: chennai

PostPosted: Sat Nov 17, 2007 11:54 am
Reply with quote

Hi Craq Giegerich,

Thank you very much. Your query is working fine
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 Need help on formatting a report DFSORT/ICETOOL 14
Search our Forums:

Back to Top