我有一个选择查询,它选择了以下两个日期/时间段之间的数据
SELECT *
FROM table1
WHERE table1 .row_creation > TO_DATE ('2017-04-03 11:00:00 AM','yyyy-mm-dd hh:mi:ss pm')
AND table1 .row_creation < TO_DATE('2017-04-04 03:00:00 AM','yyyy-mm-dd hh:mi:ss pm')
AND NOT EXISTS
(SELECT *
FROM table1
WHERE <cond>
AND table1 .row_creation > TO_DATE ('2017-04-03 11:00:00 AM','yyyy-mm-dd hh:mi:ss pm')
AND table1 .row_creation < TO_DATE('2017-04-04 03:00:00 AM','yyyy-mm-dd hh:mi:ss pm')
)
我想修改此查询以一次获取一个月中所有连续工作日的数据。谁能给我一些指导?
答案 0 :(得分:0)
如果愿意,可以在group by子句中添加更多列。
SELECT TO_CHAR(row_creation,'W')
FROM table1
WHERE
table1 .row_creation between TO_DATE ('2017-04-03 11:00:00 AM','yyyy-mm-dd hh:mi:ss pm') AND TO_DATE('2017-04-04 03:00:00 AM','yyyy-mm-dd hh:mi:ss pm')
AND NOT EXISTS
( SELECT *
FROM table1
WHERE
AND table1.row_creation BETWEEN TO_DATE ('2017-04-03 11:00:00 AM','yyyy-mm-
dd hh:mi:ss pm') AND TO_DATE('2017-04-04 03:00:00 AM','yyyy-mm-dd hh:mi:ss
pm')
)
AND TO_CHAR(row_creation,'DAY') NOT IN ('SATURDAY','SUNDAY')
GROUP BY TO_CHAR(row_creation,'W')
答案 1 :(得分:0)
我认为您可以简化整个2017年4月的操作,例如:
select *
from (
select a.*,
count(case when cond='X' then 1 end) over (partition by trunc(row_creation-3/24)) cnt
from table1 a
where row_creation - 3/24 between date '2017-04-01' and date '2017-05-01'
and to_char(row_creation, 'hh24') not between '03' and '10')
where cnt = 0
其中
row_creation - 3/24
将日期设置为00:00
-03:00
至前一天,
count(case when <cond> then 1 end) over (partition by trunc(row_creation - 3/24))
计算当天的不需要的行,最后:
where cnt = 0
如果当天至少有一个不需要的案件,负责消除行。