SQL-读取表中每天的某个时间段的数据

今天工作遇到一个问题,要统计11月上午9点到下午9点的记录。取11月的数据很容易,但是取每天的某个时间段的数据就显得有点困难,看了别人的博客,自己也学到了些东西。其实这个统计并不困难。

建立测试表

create or replace view v_ex(group_id,id,begintime,endtime,sal,ex_name) as
select 1,1,to_date('2019-11-01 01:01:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-01 01:10:02','YYYY-mm-dd hh24:mi:ss'),10,'A' from dual union all
select 1,2,to_date('2019-11-02 03:06:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-02 03:56:02','YYYY-mm-dd hh24:mi:ss'),20,'B' from dual union all
select 1,3,to_date('2019-11-03 08:06:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-03 08:46:02','YYYY-mm-dd hh24:mi:ss'),30,'C' from dual union all
select 1,4,to_date('2019-11-04 09:24:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-04 09:44:02','YYYY-mm-dd hh24:mi:ss'),40,'D' from dual union all
select 2,5,to_date('2019-11-06 10:24:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-06 10:34:02','YYYY-mm-dd hh24:mi:ss'),10,'E' from dual union all
select 2,6,to_date('2019-11-16 11:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-16 11:24:02','YYYY-mm-dd hh24:mi:ss'),20,'F' from dual union all
select 2,7,to_date('2019-11-17 21:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-17 21:56:02','YYYY-mm-dd hh24:mi:ss'),30,'G' from dual union all
select 3,8,to_date('2019-11-18 13:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-18 13:42:02','YYYY-mm-dd hh24:mi:ss'),40,'H' from dual union all
select 3,9,to_date('2019-11-19 22:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-19 22:36:02','YYYY-mm-dd hh24:mi:ss'),50,'I' from dual union all
select 3,10,to_date('2019-11-21 16:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-21 16:19:02','YYYY-mm-dd hh24:mi:ss'),10,'J' from dual union all
select 4,11,to_date('2019-11-26 17:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-26 17:18:02','YYYY-mm-dd hh24:mi:ss'),20,'K' from dual union all
select 4,12,to_date('2019-11-27 20:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-27 20:37:02','YYYY-mm-dd hh24:mi:ss'),30,'L' from dual union all
select 5,13,to_date('2019-11-28 22:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-28 22:20:02','YYYY-mm-dd hh24:mi:ss'),40,'M' from dual union all
select 5,14,to_date('2019-11-29 23:14:02','YYYY-mm-dd hh24:mi:ss'),to_date('2019-11-29 23:15:02','YYYY-mm-dd hh24:mi:ss'),50,'N' from dual;

在这里插入图片描述

统计需求

提取11月份每天的9:00-21:00的数据

select *
  from v_ex
 where begintime between
       to_date('2019-11-01 00:00:00', 'YYYY-mm-dd hh24:mi:ss') and
       to_date('2019-11-30 23:59:59', 'YYYY-mm-dd hh24:mi:ss')
   and to_char(begintime, 'hh24:mi:ss') between '09:00:00' and '21:00:00';

在这里插入图片描述

每天进步一点点!!!

发布了23 篇原创文章 · 获赞 8 · 访问量 4127

猜你喜欢

转载自blog.csdn.net/weixin_36522099/article/details/103873366
今日推荐