简体   繁体   English

从两个日期中减去某个日期

[英]subtract certain date from two dates

Let's say it is announced that 4 Aug 2022 is a public holiday.假设宣布 2022 年 8 月 4 日为公众假期。 I have a table which shows the holiday_start and holiday_end dates.我有一张显示 holiday_start 和 holiday_end 日期的表格。 How do I exclude this date "4 Aug 2022" by inputting any date i want (bind variable) and showing it as excluded day如何通过输入我想要的任何日期(绑定变量)并将其显示为排除日来排除此日期“2022 年 8 月 4 日”

eg.例如。 leave is from 01-Aug-2022 -> 10-Aug-2022 excluded days= 1休假时间为 2022 年 8 月 1 日 -> 2022 年 8 月 10 日排除天数 = 1

i've tried this:我试过这个:

SELECT :LS "Leave Start Date",
       :LE "Leave End Date",
        0 "Excluded Days"
FROM   Dual

and here is the table for reference这是供参考的表格

create table XX_LEAVES_EXCLUDES
(
exclude_id number not null primary key,
holiday_start date not null,
holiday_end date not null
);


create sequence seq_exclude_id MINVALUE 1
  START WITH 1
  INCREMENT BY 1
  CACHE 2;

create or replace trigger trg_exclude_id
before insert 
on XX_LEAVES_EXCLUDES
for each row
begin
:new.exclude_id:=seq_exclude_id.nextval;
end;



INSERT INTO XX_LEAVES_EXCLUDES (HOLIDAY_START, HOLIDAY_END) VALUES ('23-Jul-2022','20-Aug-2022');
INSERT INTO XX_LEAVES_EXCLUDES (HOLIDAY_START, HOLIDAY_END) VALUES ('01-Jul-2022','02-Aug-2022');
INSERT INTO XX_LEAVES_EXCLUDES (HOLIDAY_START, HOLIDAY_END) VALUES ('13-Jul-2022','29-Aug-2022');
INSERT INTO XX_LEAVES_EXCLUDES (HOLIDAY_START, HOLIDAY_END) VALUES ('12-Jul-2022','01-Aug-2022');
INSERT INTO XX_LEAVES_EXCLUDES (HOLIDAY_START, HOLIDAY_END) VALUES ('01-Jul-2022','29-Aug-2022');
INSERT INTO XX_LEAVES_EXCLUDES (HOLIDAY_START, HOLIDAY_END) VALUES ('08-Jul-2022','08-Aug-2022');
INSERT INTO XX_LEAVES_EXCLUDES (HOLIDAY_START, HOLIDAY_END) VALUES ('03-Jul-2022','20-Aug-2022');

If XX_LEAVES_EXCUDES is your list with leaves:如果 XX_LEAVES_EXCUDES 是您的叶子列表:

WITH XX_LEAVES_EXCLUDES AS 
(
  SELECT 1 AS exclude_id, TO_DATE('23-Jul-2022','DD-Mon-YYYY') as Start_date, TO_DATE('20-Aug-2022','DD-Mon-YYYY') as End_date FROM dual UNION ALL
  SELECT 2, TO_DATE('01-Jul-2022','DD-Mon-YYYY') as Start_date, TO_DATE('02-Aug-2022','DD-Mon-YYYY') as End_date FROM dual UNION ALL
  SELECT 3, TO_DATE('13-Jul-2022','DD-Mon-YYYY') as Start_date, TO_DATE('29-Aug-2022','DD-Mon-YYYY') as End_date FROM dual UNION ALL
  SELECT 4, TO_DATE('12-Jul-2022','DD-Mon-YYYY') as Start_date, TO_DATE('01-Aug-2022','DD-Mon-YYYY') as End_date FROM dual UNION ALL
  SELECT 5, TO_DATE('01-Jul-2022','DD-Mon-YYYY') as Start_date, TO_DATE('29-Aug-2022','DD-Mon-YYYY') as End_date FROM dual UNION ALL
  SELECT 6, TO_DATE('08-Jul-2022','DD-Mon-YYYY') as Start_date, TO_DATE('08-Aug-2022','DD-Mon-YYYY') as End_date FROM dual UNION ALL
  SELECT 7, TO_DATE('03-Jul-2022','DD-Mon-YYYY') as Start_date, TO_DATE('20-Aug-2022','DD-Mon-YYYY') as End_date FROM dual
),
HOLIDAYS AS
(
  SELECT TO_DATE('04-Aug-2022','DD-Mon-YYYY') AS excluded FROM dual
)
SELECT leaves.Start_date, leaves.End_date, NVL(excludes.excluded_days,0)
  FROM XX_LEAVES_EXCLUDES leaves
       LEFT OUTER JOIN (SELECT lvs.exclude_id, COUNT(hday.excluded) AS excluded_days
                          FROM XX_LEAVES_EXCLUDES lvs
                             , HOLIDAYS           hday
                         WHERE 1 = 1
                           AND hday.excluded BETWEEN lvs.Start_date AND lvs.End_date
                         GROUP BY lvs.exclude_id) excludes
                    ON leaves.exclude_id = excludes.exclude_id
 ORDER BY leaves.exclude_id;

I dont understand very well but assuming that "04 Aug 2022" has inserted in XX_LEAVES_EXCLUDES we have:我不太明白,但假设“2022 年 8 月 4 日”已插入 XX_LEAVES_EXCLUDES,我们有:

WITH XX_LEAVES_EXCLUDES AS (
                            select 1 as exclude_id, '04/08/2022' as holiday_start, '04/08/2022' as holiday_end from dual 
                           )
                    select 
                           :LS      as "Leave Start Date",
                           :LE      as "Leave End Date",                    
                           count(*) as "Excluded Days" 
                      from (
                            select to_date(:LS,'DD/MM/YYYY') + rownum -1 as day_by_day
                              from all_objects, XX_LEAVES_EXCLUDES X
                             where rownum <= to_date(:LE,'DD/MM/YYYY') - to_date(:LS,'DD/MM/YYYY') + 1
                           ) A,
                           XX_LEAVES_EXCLUDES B
                     where A.day_by_day between B.holiday_start AND B.holiday_end;
                     
Leave Start Date   Leave End Date    Excluded Days
------------------------------------------------------
01/08/2022         10/08/2022        1

Please me back if it's correct or specify me better the problem.如果它是正确的,请我回来或更好地指定我的问题。

Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM