隐藏

oracle中查询某段日期内的某个时间段的语句

发布:2015/8/7 13:50:07作者:管理员 来源:本站 浏览次数:1668

了解to_date ,to_char, trunc, group by 等相关知识

 
 查询表aaa中bbb在2011-8-22 至2011-8-25 中 每天10:00到11:00 内的数据 以及数目

select bbb ,count(bbb) from (
select * from aaa 
where   
rec_date between 
trunc(to_date('2011-8-22 09:00:00','yyyy-mm-dd hh24:mi:ss'))
and
trunc(to_date('2011-8-25 09:00:00','yyyy-mm-dd hh24:mi:ss'))
and 
to_char( aaa.bbb, 'hh24:mi:ss') between '09:00:00' and'10:00:00')
group  by bbb

如果是两个不同的表,确定其结构是否相同,使用union进行比较
select bbb ,count(bbb) from (
select * from aaa 
where   
rec_date between 
trunc(to_date('2011-8-22 09:00:00','yyyy-mm-dd hh24:mi:ss'))
and
trunc(to_date('2011-8-25 09:00:00','yyyy-mm-dd hh24:mi:ss'))

union 

select * from aaa
where
to_char( aaa.bbb, 'hh24:mi:ss')between
 '09:00:00' and'10:00:00')

group  by bbb