Home > Ask the Oracle Experts > SQL Questions & Answers > Running a Monday-Friday report to include weekend data
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Running a Monday-Friday report to include weekend data

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 25 January 2008

I need to run a report Monday through Friday.

  • The Monday report pulls data for the previous Friday.
  • The Tuesday report pulls data for the previous Saturday to Monday.
  • The Wednesday report pulls data for the previous Tuesday.
  • The Thursday report pulls data for the previous Wednesday.
  • The Friday report pulls data for the previous Thursday.

How can this be done?


>
EXPERT RESPONSE

My approach (subject, of course, to rigorous testing) would be something like this:

select ...
  from ...
 where sales_date in
       ( current_date - interval
           case dayofweek(current_date) 
              when 2 /* mon */ then 3
              when 3 /* tue */ then 1 
              when 4 /* wed */ then 1 
              when 5 /* thu */ then 1 
              when 6 /* fri */ then 1
                               else null end  day
       , current_date - interval
           case dayofweek(current_date) 
              when 3 /* tue */ then 2 
                               else null end  day
       , current_date - interval
           case dayofweek(current_date) 
              when 3 /* tue */ then 3 
                               else null end  day )

The DAYOFWEEK function does not exist with that actual name in all database systems, so have a look and see what functions are available to you. The important part of the approach is the use of the IN list, containing up to 3 dates which your column will be compared to.

Let's look at the first CASE expression. On Monday, subtracting 3 days yields the Friday before, and on Tuesday through Friday, subtracting 1 day yields the respective day before. That's the first date in the IN list. The other two dates in the IN list will be NULL except on Tuesday when the second date will be the Sunday before, and the third date will be the Saturday before.

Why NULL? Because the subtraction CURRENT_DATE - INTERVAL n DAY will return NULL if n is NULL. Any calculation involving NULL returns NULL (NULL can be called "greedy" in this regard). Thus the IN list will resolve to something like:

 where sales_date in
       ( '2008-02-29'
       , NULL
       , NULL )

Note that if you accidentally run the report on Saturday or Sunday, where the day of the week is 7 or 1, all three CASE expressions return NULL, and no data will be extracted at all.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL

Oracle development languages
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts