View previous topic :: View next topic
|
Author |
Message |
chandracdac
New User
Joined: 15 Jun 2007 Posts: 92 Location: bangalore
|
|
|
|
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 |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10872 Location: italy
|
|
|
|
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 |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
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 |
|
|
senthilkuduva
New User
Joined: 16 Nov 2007 Posts: 2 Location: chennai
|
|
|
|
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 |
|
|
chandracdac
New User
Joined: 15 Jun 2007 Posts: 92 Location: bangalore
|
|
|
|
Thanks to all |
|
Back to top |
|
|
chandracdac
New User
Joined: 15 Jun 2007 Posts: 92 Location: bangalore
|
|
|
|
Thanks craq its working.. |
|
Back to top |
|
|
senthilkuduva
New User
Joined: 16 Nov 2007 Posts: 2 Location: chennai
|
|
|
|
Hi Craq Giegerich,
Thank you very much. Your query is working fine |
|
Back to top |
|
|
|