Oracle SQL删除星期日,只考虑6个工作日

apeeds0o  于 2023-08-03  发布在  Oracle
关注(0)|答案(1)|浏览(139)

我尝试了这个SQL CASE来获取剩余的天数超过KPI(OKPI)。
有两个主要的情况让我很困惑,
1-删除或不考虑星期天作为一个工作日。
2-实际bkg_date在1天之后。例如,如果bkg_date = '08 Jul 2023',则bkg_date将= '10 Jul 2023'

CASE
    WHEN SYSDATE - (x.bkg_date + 1) <= x.dlvry_kpi THEN
        CASE
            WHEN x.dlvry_kpi - (
                -- Full weeks from Monday of start week to Monday of current week
                (TRUNC(SYSDATE, 'IW') - TRUNC(x.bkg_date + 1, 'IW')) * 6 / 7
                -- Add extra days in the current week excluding Sunday
                + CASE WHEN TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW') + 1 <= 6 THEN TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW') + 1 ELSE 6 END
                -- Subtract days in the week before the start date excluding Sunday
                - CASE WHEN TRUNC(x.bkg_date + 1) - TRUNC(x.bkg_date + 1, 'IW') <= 6 THEN TRUNC(x.bkg_date + 1) - TRUNC(x.bkg_date + 1, 'IW') ELSE 6 END
            ) <= 0 THEN '00 days left'
            ELSE
                TO_CHAR(
                    x.dlvry_kpi - (
                        -- Full weeks from Monday of start week to Monday of current week
                        (TRUNC(SYSDATE, 'IW') - TRUNC(x.bkg_date + 1, 'IW')) * 6 / 7
                        -- Add extra days in the current week excluding Sunday
                        + CASE WHEN TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW') + 1 <= 6 THEN TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW') + 1 ELSE 6 END
                        -- Subtract days in the week before the start date excluding Sunday
                        - CASE WHEN TRUNC(x.bkg_date + 1) - TRUNC(x.bkg_date + 1, 'IW') <= 6 THEN TRUNC(x.bkg_date + 1) - TRUNC(x.bkg_date + 1, 'IW') ELSE 6 END
                    )
                ) || ' days left'
        END
    ELSE 'OKPI'
END AS AGING

字符串
所以,这就是我提出的,并给出了以下输入的结果OKPI;
x.bkg_date =“2023年7月8日”
sysdate = '2023年7月17日'
x.dlvry_kpi = 7

ltqd579y

ltqd579y1#

您可以使用以下命令查找下一个工作日(跳过星期日):

bkg_date + CASE TRUNC(bkg_date) - TRUNC(bkg_date, 'IW') WHEN 5 THEN 2 ELSE 1 END

字符串
对于示例数据:

CREATE TABLE table_name (bkg_date, dlvry_kpi) AS
  SELECT TRUNC(SYSDATE, 'IW') + 8 - LEVEL, 1 FROM DUAL CONNECT BY LEVEL <= 15;


然后:

SELECT bkg_date,
       next_bkg_date,
       dlvry_kpi,
       TRUNC(SYSDATE) AS today,
       (TRUNC(next_bkg_date, 'IW') - TRUNC(SYSDATE, 'IW')) * 6 / 7 AS full_weeks,
       LEAST(TRUNC(next_bkg_date) - TRUNC(next_bkg_date, 'IW'), 5) AS days_at_end,
       LEAST(TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW'), 5) AS day_at_start,
       -- Full weeks from Monday of current week to Monday of end week
       (TRUNC(next_bkg_date, 'IW') - TRUNC(SYSDATE, 'IW')) * 6 / 7
       -- Add extra days in the end week excluding Sunday
       + LEAST(TRUNC(next_bkg_date) - TRUNC(next_bkg_date, 'IW'), 5)
       -- Subtract days in the current week before the today excluding Sunday
       - LEAST(TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW'), 5) AS days
FROM   (
  SELECT bkg_date,
         bkg_date + CASE TRUNC(bkg_date) - TRUNC(bkg_date, 'IW') WHEN 5 THEN 2 ELSE 1 END as next_bkg_date,
         dlvry_kpi
  FROM   table_name
)


输出:
| 下一个_BKG_日期|DLVRY_KPI|今天|完整_周|结束时天数|开始日期|天数| DAYS |
| --|--|--|--|--|--|--| ------------ |
| 2023-07-25 00:00:00(星期二)|一个|2023-07-17 00:00:00(星期一)|六个|一个|0个|七个| 7 |
| 2023-07-24 00:00:00(星期一)|一个|2023-07-17 00:00:00(星期一)|六个|0个|0个|六个| 6 |
| 2023-07-24 00:00:00(星期一)|一个|2023-07-17 00:00:00(星期一)|六个|0个|0个|六个| 6 |
| 2023-07-22 00:00:00(星期六)|一个|2023-07-17 00:00:00(星期一)|0个|五个|0个|五个| 5 |
| 2023-07-21 00:00:00(FRI)|一个|2023-07-17 00:00:00(星期一)|0个|四个|0个|四个| 4 |
| 2023-07-20 00:00:00(星期四)|一个|2023-07-17 00:00:00(星期一)|0个|三个|0个|三个| 3 |
| 2023-07-19 00:00:00(星期三)|一个|2023-07-17 00:00:00(星期一)|0个|二个|0个|二个| 2 |
| 2023-07-18 00:00:00(星期二)|一个|2023-07-17 00:00:00(星期一)|0个|一个|0个|一个| 1 |
| 2023-07-17 00:00:00(星期一)|一个|2023-07-17 00:00:00(星期一)|0个|0个|0个|0个| 0 |
| 2023-07-17 00:00:00(星期一)|一个|2023-07-17 00:00:00(星期一)|0个|0个|0个|0个| 0 |
| 2023-07-15 00:00:00(星期六)|一个|2023-07-17 00:00:00(星期一)|-6|五个|0个|-1| -1 |
| 2023-07-14 00:00:00(FRI)|一个|2023-07-17 00:00:00(星期一)|-6|四个|0个|-2| -2 |
| 2023-07-13 00:00:00(星期四)|一个|2023-07-17 00:00:00(星期一)|-6|三个|0个|-3| -3 |
| 2023-07-12 00:00:00(星期三)|一个|2023-07-17 00:00:00(星期一)|-6|二个|0个|-4| -4 |
| 2023-07-11 00:00:00(星期二)|一个|2023-07-17 00:00:00(星期一)|-6|一个|0个|-5| -5 |
然后得到剩下的天数:

SELECT bkg_date,
       dlvry_kpi,
       days,
       CASE
       WHEN days < -dlvry_kpi
       THEN 'OKPI'
       ELSE TO_CHAR(dlvry_kpi + days, 'FM90') || ' days left'
       END AS aging
FROM   (
  SELECT bkg_date,
         dlvry_kpi,
         -- Full weeks from Monday of current week to Monday of end week
         (TRUNC(next_bkg_date, 'IW') - TRUNC(SYSDATE, 'IW')) * 6 / 7
         -- Add extra days in the end week excluding Sunday
         + LEAST(TRUNC(next_bkg_date) - TRUNC(next_bkg_date, 'IW'), 5)
         -- Subtract days in the current week before the today excluding Sunday
         - LEAST(TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW'), 5) AS days
  FROM   (
    SELECT bkg_date,
           bkg_date + CASE TRUNC(bkg_date) - TRUNC(bkg_date, 'IW') WHEN 5 THEN 2 ELSE 1 END as next_bkg_date,
           dlvry_kpi
    FROM   table_name
  )
)


哪些输出:
| DLVRY_KPI|天数|老化| AGING |
| --|--|--| ------------ |
| 一个|七个|8天left| 8 days left |
| 一个|六个|7天left| 7 days left |
| 一个|六个|7天left| 7 days left |
| 一个|五个|6天left| 6 days left |
| 一个|四个|5天left| 5 days left |
| 一个|三个|4天left| 4 days left |
| 一个|二个|3天left| 3 days left |
| 一个|一个|2天left| 2 days left |
| 一个|0个|1天left| 1 days left |
| 一个|0个|1天left| 1 days left |
| 一个|-1| 0天left| 0 days left |
| 一个|-2| OKPI| OKPI |
| 一个|-3| OKPI| OKPI |
| 一个|-4| OKPI| OKPI |
| 一个|-5| OKPI| OKPI |
fiddle

相关问题