定义逻辑以计算从02:00:00到02:00:00 AM的日期

时间:2017-03-13 07:05:27

标签: sql oracle

我有以下sql查询得到一整天的总小时数, 此查询工作正常,但我们的对象的日期从第二天的02:00:00到02:00:00开始,在此查询中,它按正常工作日的上午12:00:00 AM到12:59进行分组:下午59点。 如何在上午02:00:00到02:00:00之间添加逻辑到组日间?

    select object_code, 
    sum(on_stream_hrs) on_stream_hrs,
    daytime 
from(select object_code, 
            on_stream_hrs, 
            trunc(daytime) daytime 
     from sub_day_status 
     where trunc(daytime) between to_date('1 Feb 2017') and to_date('28 Feb 2017')
     ) 
where (object_code, daytime) in (select object_code, 
                                    trunc(daytime) 
                                 from  sub_day_status 
                                 where trunc(daytime) between to_date('1 Feb 2017') and to_date('28 Feb 2017') 
                                 and on_stream_hrs >0.5)
group by object_code, daytime 
order by daytime

感谢, 小号

2 个答案:

答案 0 :(得分:0)

  

to_date('2017年2月1日')和to_date('28 Feb)之间的截断(白天)   2017' )

您可以通过以下方式将日期移动2小时。

select to_date('1 Feb 2018', 'DD MON YYYY') + 2/24 from dual

Reference

答案 1 :(得分:0)

在截断前从daytime减去2小时:

SELECT object_code, 
       SUM( on_stream_hrs ) AS on_stream_hrs,
       TRUNC( daytime - INTERVAL '2' HOUR ) AS daytime
FROM   sub_day_status
WHERE  daytime >= DATE '2017-02-01' + INTERVAL '2' HOUR
AND    daytime <  DATE '2017-03-01' + INTERVAL '2' HOUR
GROUP BY
       object_code, 
       TRUNC( daytime - INTERVAL '2' HOUR )
HAVING MAX( on_stream_hrs ) > 0.5
ORDER BY
       daytime;

相关的子查询似乎可以使用简单的WHERE on_stream_hrs > 0.5或使用HAVING子句(如上所述),具体取决于您希望如何过滤行。