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';

在这里插入图片描述

每天进步一点点!!!

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值