Solved

calculate business days (sql, oracle)

Posted on 2014-02-13
2
456 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 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39856561
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
ID: 39856606
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Repeat query 13 61
PL SQL Developer 7 54
Creation date for a PDB 5 39
setting local variables in a cursor block 3 20
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

830 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