我有一个带有date_create的表 - 日期类型(YYYY-MM-DD HH:MM:SS)和id_number。 我需要计算在两个班次之间创建的id的数量 - 从早上8点到晚上8点以及从晚上8点到早上8点计算它们并按照exatc轮班日计算。我该怎么办?
答案 0 :(得分:1)
我使用cte
生成的一些示例数据with cte as
(
select 1 as id,sysdate+(1/24) as date1 from dual
union all
select 2 as id,sysdate+(2/24) date1 from dual
union all
select 4 as id,sysdate+(1/24) date1 from dual
union all
select 3 as id,sysdate+(3/24) date1 from dual
union all
select 5 as id,sysdate+(3/24) date1 from dual
union all
select 8 as id,sysdate+(9/24) date1 from dual
union all
select 6 as id,sysdate+(9/24) date1 from dual
union all
select 7 as id,sysdate+(17/24) date1 from dual
)
select * from cte
执行上面的结果后我们得到了一些样本数据
ID DATE1
1 November, 30 2015 07:02:53
2 November, 30 2015 08:02:53
4 November, 30 2015 07:02:53
3 November, 30 2015 09:02:53
5 November, 30 2015 09:02:53
8 November, 30 2015 15:02:53
6 November, 30 2015 15:02:53
7 November, 30 2015 23:02:53
在此之后我执行了以下查询以及上面的cte。
SELECT count(created_id) created_count
,CASE
WHEN date1 BETWEEN 8
AND 20
THEN '8 am to 8pm'
ELSE '9 pm to 7 am'
END type_1
FROM (
SELECT id AS created_id
,to_number(to_char(date1, 'HH24')) date1
FROM cte
) a
GROUP BY CASE
WHEN date1 BETWEEN 8
AND 20
THEN 0
ELSE 1
END
,CASE
WHEN date1 BETWEEN 8
AND 20
THEN '8 am to 8pm'
ELSE '9 pm to 7 am'
END
<强>结果
CREATED_COUNT TYPE_1
5 8 AM TO 8PM
3 9 PM TO 7 AM
注意:上面的示例我已经完成了我的样本数据以及您在帖子中提供的详细信息。
答案 1 :(得分:0)
检查此查询。它可能会对你有帮助。
SELECT COUNT(1),
shift
FROM
(SELECT
CASE
WHEN TO_DATE(TO_CHAR(sht_time,'HH24:MI'),'HH24:MI') BETWEEN TO_DATE('08:00','HH24:MI') AND TO_DATE('20:00','HH24:MI')
THEN '8am_8pm'
ELSE '8pm_8am'
END shift,
ID_NO
FROM emp_sft
)a
GROUP BY A.SHIFT;