1.确定两个日期之间的工作日天数
--确定两个日期之间的工作日天数
with x0 as
(select to_date(‘2018-01-01‘,‘yyyy-mm-dd‘) as 日期 from dual
union all
select to_date(‘2018-01-15‘,‘yyyy-mm-dd‘) as 日期 from dual ),
x1 as --日期并列显示
(select min (日期) 开始日期,max(日期) 结束日期 from x0 ),
x2 as --日期之间的天数
(select 结束日期-开始日期+1 as 天数 ,开始日期,结束日期 from x1),
x3 as --构造一个从开始日期到结束日期的日期集合 (枚举日期)
(select to_char(开始日期+level-1,‘DY‘) as 枚举日期 from x2 connect by level <=天数)
--统计日期
select sum(case when 枚举日期 in (‘sat‘,‘sun‘)then 0 else 1 end ) as 工作日 from x3
2.计算一年周内各日期次数
--计算一年周内各日期次数
with x0 as
(select to_date(‘2018-01-01‘,‘yyyy-mm-dd‘) as 年初 from dual ),
x1 as
(select 年初,add_months(年初,12) as 下年年初 from x0),
x2 as
(select 年初,下年年初,下年年初-年初 as 天数 from x1),
x3 as
(select 年初+level-1 as 日期 from x2 connect by level <=天数 ),
x4 as
(select 日期,to_char(日期,‘DAY‘) AS 星期 FROM X3)
SELECT 星期,count(*) from x4 group by 星期
3.确定当前记录和下一条记录之间的相差天数
with x0 as (select ‘1‘as name ,to_date(‘2018-01-01‘,‘yyyy-mm-dd‘) as 日期 from dual union all select ‘2‘ as name,to_date(‘2018-01-15‘,‘yyyy-mm-dd‘) as 日期 from dual union all select ‘3‘ as name, to_date(‘2018-01-26‘,‘yyyy-mm-dd‘) as 日期 from dual ) select name ,日期,next_d,(next_d-日期) as 相差天数 from ( select name ,日期, lead(日期,1,null) over (order by 日期) as next_d from x0 )
lead 函数见下文说明:(014)每日SQL学习:oracle下lag和lead分析函数