各位前輩好
請問目前我有一個TABLE DAY_OFF來紀錄非工作日
只要有在DAY_OFF這個TABLE的日期就是非工作日
若我要找當前日期上一個工作天
例如:
假如是2022/6/9得到=>2022/6/8
假如是2022/6/6得到=>2022/6/2
可以如何達成,謝謝!
Table結構可看成以下
工休日 工休狀態
df_date | status
------------+------------
2022-06-03 | 0
2022-06-04 | 1
2022-06-05 | 2
2022-06-10 | 1
2022-06-11 | 2
create table it0610 (
df_date date
);
insert into it0610 values (to_date('2022-06-03', 'yyyy-mm-dd'));
insert into it0610 values (to_date('2022-06-04', 'yyyy-mm-dd'));
insert into it0610 values (to_date('2022-06-05', 'yyyy-mm-dd'));
insert into it0610 values (to_date('2022-06-10', 'yyyy-mm-dd'));
insert into it0610 values (to_date('2022-06-11', 'yyyy-mm-dd'));
with t1(n) as (
select 1
from dual
union all
select n + 1
from t1
where n < 10
), t2 (be_date) as (
SELECT to_date('2022-06-06', 'yyyy-mm-dd') - n
FROM t1
), t3 (be_date) as (
select be_date
from t2
minus
select df_date
from it0610
)
select max(be_date)
from t3;
很抱歉~這個我只有想到鳥招....看其他高手有沒有更好解法...0.-|||
用善用的MSSQL...
create table DAY_OFF(
df_date date
,status int
);
insert into DAY_OFF
values('2022-06-03',0)
,('2022-06-04',1)
,('2022-06-05',2)
,('2022-06-10',1)
,('2022-06-11',2);
create table Test(
DateTmp date
);
insert into Test
values('2022-06-09')
,('2022-06-06')
select *
,isNull((
select
isNull((
select isNull((
select isNull((
select dateadd(d,-1,d.df_date)
from DAY_OFF d
where dateadd(d,-1,c.df_date) = d.df_date
),dateadd(d,-1,c.df_date))
from DAY_OFF c
where dateadd(d,-1,b.df_date) = c.df_date
),dateadd(d,-1,b.df_date))
from DAY_OFF b
where dateadd(d,-1,a.df_date) = b.df_date
),dateadd(d,-1,a.df_date))
from DAY_OFF a
where a.df_date = dateadd(d,-1,DateTmp)
),dateadd(d,-1,DateTmp)) DatePre
from test
Demo
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=f95bd52fa6ce273c0ab6d60f79fe2793
with dd as (
select to_date('2022-06-06', 'yyyy-mm-dd') target
FROM dual
) select min( d_day ) from (select dd.target -level d_day from dual, dd
connect by dd.target - level +1 in ( select df_date from it0610
where df_date < dd.target ));
with dd as (
select to_date('2022-06-09', 'yyyy-mm-dd') target
FROM dual
) select min( d_day ) from (select dd.target -level d_day from dual, dd
connect by dd.target - level +1 in ( select df_date from it0610
where df_date < dd.target ));
有趣的問題.. 借樓上的資料試了一下 不過我沒很仔細的驗過
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=8f00226140c79ea9fc2c966cc902105a