Solved

Oracle syntax for  Last four  business workings days

Posted on 2014-12-19
8
86 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

758 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

21 Experts available now in Live!

Get 1:1 Help Now