计算两个细分之间的天数

时间:2018-10-24 20:08:24

标签: sql oracle calendar between

我在下面有两个表格。我只想计算从Hire_dt到雇用日期所在的日历月末之间的星期一至星期五的天数。

TableA

Hire_DT         Id
 09/26/2018     1

TableCalendar:

Date         WorkDay(M-F)      EOM        WorkDay
09/26/2018     Wednesday       9/30/2018    1
09/27/2018     Thursday        09/30/2018   1
09/28/2018     Friday          09/30/2018   1
09/29/2018     Saturday        09/30/2018   0
09/30/2018     Sunday          09/30/2018   0

预期结果

Hire_dt        WorkDaysEndMonth   WorkDaysEndMonth --counting hire_dt
 09/26/2018         2                 3

2 个答案:

答案 0 :(得分:5)

这是进行计算的一种方法-无需使用日历表。唯一的输入数据是来自第一个表(ID和HIRE_DATE)的数据,我将其包含在WITH子句中(这不是回答您问题的查询的一部分!)。其他所有内容均已计算。我展示了如何计算包括雇用日期在内的天数;如果不需要,请在末尾减去1。

TRUNC(<date>, 'iw')<date>中一周中的星期一。该查询将计算从星期一到EOM的EOM周中有多少天,但不超过5天(以防EOM可能是星期六或星期日)。它对HIRE_DATE进行了类似的计算,但是它计算了从星期一到HIRE_DATE 不包括 HIRE_DATE的天数。最后一部分是在HIRE_DATE星期一至EOM星期一之间的每整周增加5天。

with
  sample_data(id, hire_date) as (
    select 1, to_date('09/26/2018', 'mm/dd/yyyy') from dual union all
    select 2, to_date('07/10/2018', 'mm/dd/yyyy') from dual
  )
select id, to_char(hire_date, 'Dy mm/dd/yyyy')                      as hire_date,
       to_char(eom, 'Dy mm/dd/yyyy')                                as eom,
       least(5, eom - eom_mon + 1) - least(5, hire_date - hire_mon)
       + (eom_mon - hire_mon) * 5 / 7                               as workdays
from   (
         select id, hire_date, last_day(hire_date) as eom,
                trunc(hire_date, 'iw')             as hire_mon,
                trunc(last_day(hire_date), 'iw')   as eom_mon
         from   sample_data
       )
;

        ID HIRE_DATE               EOM                       WORKDAYS
---------- ----------------------- ----------------------- ----------
         1 Wed 09/26/2018          Sun 09/30/2018                   3
         2 Tue 07/10/2018          Tue 07/31/2018                  16

答案 1 :(得分:0)

您可以使用以下例程(其中set.seed(1) x <- rnorm(1e4, 0, 5) y <- rnorm(1e4, 0, 10) dist <- sqrt(x^2 + y^2) z <- exp(-(dist / 8)^2) library(tidyverse) a1 <- data.frame(x, y, z) %>% ggplot() + geom_point(aes(x, y, color = z)) b1 <- data.frame(x, y, z) %>% ggplot( aes(x, y, z=z )) + stat_summary_hex(fun = function(x) mean(x)) c1 <- data.frame(x, y, z) %>% ggplot( aes(x, y, z=z )) + stat_summary_hex(fun = function(x) mean(x), bins = 60) multiplot(a1,b1,c1, cols = 3) 函数是重要的贡献者):

x <- rnorm(1e4, 0, 5)                                                     
y <- rnorm(1e4, 0, 10)                                                    
dist <- sqrt(x^2 + y^2)                                                   
z <- exp(-(dist / 8)^2) 

P.S。来自last_day的整数值取决于SQL> alter session set NLS_TERRITORY="AMERICA"; SQL> create table TableA( ID int, Hire_DT date ); SQL> insert into TableA values(1,date'2018-09-26'); SQL> select sum(case when mod(to_char(myDate,'D'),7) <= 1 then 0 else 1 end ) as "WorkDaysEndMonth" from ( select Hire_DT + level - 1 myDate from TableA where ID = 1 connect by level <= last_day(Hire_DT) - Hire_DT + 1 ); WorkDaysEndMonth ---------------- 3 的设置。在这里,我使用to_char(<date>,'D'),其中NLS_TERRITORYAMERICA,而Saturday7th天,而Sunday(或我的国家{{ 1}})分别设置为1stUNITED KINGDOM

Rextester Demo