Solved

Oracle syntax for  Last four  business workings days

Posted on 2014-12-19
8
88 Views
Last Modified: 2014-12-19
Hi Experts

Can anyone help with the PL/SQL syntax to show dates that are within the last four business days on a where clause.

Many thanks!
0
Comment
Question by:MrDavidThorn
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40508903
as business days would vary depending on the business, country etc, the only real solution is to use a calendar table which, for each date, indicates if it is a business day (for you) or not.
starting from there (http://www.experts-exchange.com/Database/MS-SQL-Server/A_12267-Date-Fun-Part-One-Build-your-own-SQL-calendar-table-to-perform-complex-date-expressions.html), you would query like this:
with data as ( select PKDate , row_number() over ( order by PKDate  desc) rn from days  where PKDate  < trunc(sysdate) and is_workday = 1 ) select * from data where rn <= 4 

Open in new window

this is it about what you need to do, in short
0
 

Author Comment

by:MrDavidThorn
ID: 40508911
Well the Business week is Monday to Friday, so I want to avoid Saturday / Sunday
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40508923
is this the case for all your weeks? or will the official holidays not be skipped also?
0
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.

 
LVL 34

Accepted Solution

by:
johnsone earned 250 total points
ID: 40508925
Assuming that you don't want to account for holidays, and only care about Saturday/Sunday, then this should work.
SELECT d 
FROM   (SELECT d 
        FROM   (SELECT Trunc(SYSDATE) - LEVEL + 1 d 
                FROM   dual 
                CONNECT BY LEVEL <= 7) 
        WHERE  Trim(To_char(d, 'Day')) NOT IN ( 'Saturday', 'Sunday' ) 
        ORDER  BY d) 
WHERE  ROWNUM <= 4; 

Open in new window

0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40508928
ORDER  BY d)
should that not be :
ORDER  BY d desc)
0
 
LVL 34

Expert Comment

by:johnsone
ID: 40508930
Yes, I somehow missed that in the cut and paste.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40509265
Just to be clear, if today were Tuesday, you want a where clause that would select records with a date of yesterday, last Friday, Thursday, or Wednesday?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40509278
Or this (just subtract the holidays):
(  GREATEST ( NEXT_DAY ( v_start_date, 'MON') - v_start_date - 2, 0)
          + ((NEXT_DAY ( v_end_date, 'MON') - NEXT_DAY ( v_start_date, 'MON'))  / 7)  * 5)
          -  (GREATEST ( NEXT_DAY ( v_end_date, 'MON') - v_end_date - 3, 0))
          - Holidays

Open in new window

:p
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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

773 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