Solved

calculate business days (sql, oracle)

Posted on 2014-02-13
2
450 Views
Last Modified: 2014-02-13
How do I restrict a query results to a certain number of business days? For example, if I need something fort he past 15 business days.
0
Comment
Question by:teaone
2 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
this will return the past 15 business days (meaning all days except Saturday and Sunday)
including today.


SELECT *
  FROM (SELECT *
          FROM (SELECT SYSDATE - LEVEL - 1 d
                  FROM DUAL
                CONNECT BY LEVEL <= 21)
         WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun')
        ORDER BY d DESC)
 WHERE ROWNUM <= 15

You could then use this to join to your other table

Alternatively you could generate a range...

SELECT min(d), max(d)
  FROM (SELECT *
          FROM (SELECT SYSDATE - LEVEL - 1 d
                  FROM DUAL
                CONNECT BY LEVEL <= 21)
         WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun')
        ORDER BY d DESC)
 WHERE ROWNUM <= 15;

and then use this where your table between those values; but then you'd also have to exlude Saturday and Sunday using a condition similar to that shown
0
 

Author Closing Comment

by:teaone
Comment Utility
thank you!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now