【问题标题】:Counting working days between two dates Oracle计算两个日期之间的工作日 Oracle
【发布时间】:2018-12-17 11:57:15
【问题描述】:

我正在尝试编写一个查询,它可以计算收到付款和处理付款之间的工作日数,我开始在 2017 年 12 月收到付款;

select unique trunc(date_received), 
   (case when trunc(date_received) in ('25-DEC-17','26-DEC-17') Then 0 when 
to_char(date_received,'D') <6 Then 1 else 0 end) Working_day
from payments
where date_received between '01-DEC-17' and '31-dec-17'
order by trunc(date_received) 

但老实说,我不知道如何进一步处理并添加 date_processed 并计算 date_processed 和 date_received 之间的工作日数......任何帮助将不胜感激......

【问题讨论】:

  • 可以参考这个链接Count Work Days
  • 我推荐一个日历表,有指定的工作日和非工作日。
  • 并在该日历表中添加一列workday_number,其运行编号(使用超过 1/0 的累积总和)仅在工作日增加。然后,您的计算基于此日历的两个联接(date_receiveddate_processed)和一个简单的t1.workday_number - t2.workday_number

标签: sql oracle


【解决方案1】:

也许不是最理想的,但它工作得很好,而且很容易合并更复杂的检查,比如假期。此查询首先生成两个日期之间的所有日期,然后让您过滤掉所有“不计入”的日期。

在这个实现中,我只过滤掉了周末,但是很容易添加对假期等的检查。

with 
  -- YourQuery: I used a stub, but you can use your actual query here, which 
  -- returns a from date and to date. If you have multiple rows, you can also
  -- output some id here, which can be used for grouping in the last step.
  YourQuery as
  ( 
    select 
      trunc(sysdate - 7) as FromDate,
      trunc(sysdate) as ToDate
    from dual),

  -- DaysBetween. This returns all the dates from the start date up to and
  -- including the end date.
  DaysBetween as
  (  
    select
      FromDate,
      FromDate + level - 1 as DayBetween,
      ToDate
    from
      YourQuery
    connect by
      FromDate + level - 1 <= ToDate)

-- As a last step, you can filter out all the days you want. 
-- This default query only filters out Saturdays and Sundays, but you
-- could add a 'not exists' check that checks against a table with known 
-- holidays.
select
  count(*)
from
  DaysBetween d
where
  trim(to_char(DAYINBETWEEN, 'DAY', 'NLS_DATE_LANGUAGE=AMERICAN'))
    not in ('SATURDAY', 'SUNDAY');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-20
    • 2018-08-03
    • 2014-10-20
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多