oracle 如何仅打印两个日期之间的选定(输出)日

fjaof16o  于 2023-03-07  发布在  Oracle
关注(0)|答案(1)|浏览(218)

此代码输出一周中的不同日期,即mon、tue等。例如,输出可以仅为mon,或mon、tue、wed、thu和fri

school_days AS
  (
     SELECT to_char(trunc(sysdate ,'D') + LEVEL - sw.LEV_1, 'dy') as school_day
       FROM SCHOOL_WEEKS sw
    CONNECT BY LEVEL <= sw.LEV_2
  ),

此代码输出开始日期和结束日期之间的所有日期

all_dates AS 
( 
    SELECT sw.sc_start_date + LEVEL-1 as weekday_date, to_char((sw.sc_start_date + LEVEL-1), 'DAY') as weekday_day
      FROM SCHOOL_WEEKS sw
   CONNECT BY sw.sc_start_date + LEVEL-1 <=  sw.sc_end_date
)

如何只打印两个日期之间的选定日期?

SELECT * FROM all_dates
 WHERE to_char(weekday_date, 'dy') IN ('output of school_days only')
bpzcxfmw

bpzcxfmw1#

schoolday_DATE添加到第一个查询,然后根据DATE数据类型值将它们连接起来:

with
school_days AS
  (SELECT to_char(trunc(sysdate ,'D') + LEVEL - sw.LEV_1, 'dy') as school_day,
                  trunc(sysdate ,'D') + LEVEL - sw.LEV_1        as school_date
       FROM SCHOOL_WEEKS sw
    CONNECT BY LEVEL <= sw.LEV_2
  ),
all_dates AS 
  (SELECT to_char((sw.sc_start_date + LEVEL-1), 'DAY') as weekday_day,
                   sw.sc_start_date + LEVEL-1          as weekday_date           
      FROM SCHOOL_WEEKS sw
   CONNECT BY sw.sc_start_date + LEVEL-1 <=  sw.sc_end_date
  )
select a.*
from all_dates a join school_days s on s.school_date = a.weekday_day
order by a.weekday_date;

相关问题