计算日期范围内的星期六和星期日的数量 - oracle [重复]
Posted
技术标签:
【中文标题】计算日期范围内的星期六和星期日的数量 - oracle [重复]【英文标题】:Count the no of saturdays and sundays in date range - oracle [duplicate] 【发布时间】:2014-08-20 08:25:17 【问题描述】:我有两个参数(start_Date,end_Date)来自 table1
我正在尝试计算日期范围内的星期六和星期日的数量
star_Date=8/20/2014 13:52
end_Date=8/28/2014 13:52
结果应该是这样的
Start_Date end_date No_of_leaves
8/20/2014 13:52 8/28/2014 13:52 2
更新部分
SELECT retouch_req_time,retouch_submit_time,(
SELECT Count(*) FROM (SELECT To_char(start_date + ( LEVEL - 1 ), 'fmday') dt
FROM (WITH t AS (SELECT To_date (retouch_req_time, 'MM/DD/YYYY HH24:MI') start_date, To_date (retouch_submit_time, 'MM/DD/YYYY HH24:MI') end_date FROM TT))
CONNECT BY LEVEL <= end_date - start_date + 1) WHERE dt IN ('friday','saturday')) as worked_hours
FROM TT
【问题讨论】:
【参考方案1】:您可以尝试使用分层查询
WITH t
AS (SELECT To_date ('8/20/2014 13:52', 'MM/DD/YYYY HH24:MI') start_date,
To_date ('8/28/2014 13:52', 'MM/DD/YYYY HH24:MI') end_date
FROM dual)
SELECT Count(*)
FROM (SELECT To_char(start_date + ( LEVEL - 1 ), 'fmday') dt
FROM t
CONNECT BY LEVEL <= end_date - start_date + 1)
WHERE dt IN ( 'friday', 'saturday' );
RESULT
------
2
* 通过扩大范围列出日期。 * TO_CHAR 函数用于获取工作日 * 计算星期五或星期六的所有内容
如果你想计算日期,那么你可以试试
SELECT To_char(dat, 'DY'),
Count(*)
FROM (SELECT To_date ('8/20/2014 13:52', 'MM/DD/YYYY HH24:MI')
+ num dat
FROM (SELECT LEVEL - 1 num
FROM dual
CONNECT BY LEVEL <= Abs(To_date ('8/20/2014 13:52',
'MM/DD/YYYY HH24:MI') -
To_date (
'8/28/2014 13:52'
,
'MM/DD/YYYY HH24:MI')) - 1
))
WHERE To_char(dat, 'DY') IN ( 'FRI', 'SAT' )
GROUP BY To_char(dat, 'DY');
RESULTS
TO_CHAR(DAT,'DY') COUNT(*)
----------------- --------
FRI 1
SAT 1
【讨论】:
@realsprituals 我修改了你的第一个查询并运行它.. 它给出了缺少选择错误.. 你能帮忙.. 我在我的问题的更新部分更新我的查询 你不能像这样使用公用表表达式。【参考方案2】:您可以像这样计算星期六和星期日的数量:
with t(d) as (
select sysdate + level from dual connect by rownum < 10
)
select count(case when trim(to_char(d, 'DAY')) in ('SATURDAY', 'SUNDAY') then 1 end) cnt from t
CNT
---
2
如果您没有日期范围,那么:
with t(a, b) as (
select sysdate a, sysdate + 10 b from dual connect by rownum < 10
), t2(d) as (
select a + level - 1 from t connect by rownum <= b - a
)
select count(case when trim(to_char(d, 'DAY')) in ('SATURDAY', 'SUNDAY') then 1 end) cnt from t2
CNT
---
2
【讨论】:
以上是关于计算日期范围内的星期六和星期日的数量 - oracle [重复]的主要内容,如果未能解决你的问题,请参考以下文章
给定日期范围(开始日期和结束日期),我如何计算天数,不包括 .Net 中指定的星期几?
Oracle查询一段日期内的星期的日期..例如我要查2011-3-18至2011-3-30内所有星期一的日期..应该怎样查询