2013-08-23 42 views
1

我无法找到解决方案如何使用Oracle中的Select语句和group by。Oracle按工作时间段和非工作时间段

我需要按工作时间段(“当天”的09:00至21:00)和第二个时间段(前一天的21:01至“当天”的08:59)的时间戳列进行分组 在一个mounth内。

欢迎任何建议。 谢谢。

回答

1

此分组的关键是从日期时间中减去9小时以获取“工作日”,然后使用hour函数来确定它是工作小时还是其他东西。这里有一个例子:

select trunc(worktime - 9.0/24) as workdate, 
     (case when hour(worktime) between 8 and 20 then 'WorkHours' else 'OtherHours' end), 
     count(*) 
from t 
group by trunc(worktime - 9.0/24), 
     (case when hour(worktime) between 8 and 20 then 'WorkHours' else 'OtherHours' end); 

要查看某个月,你可能想使用workdate而不是实际的日期(如此前九个小时月份真的较上月的一部分)。

0
group by 
     trunc(timestamp_col, 'MM') 
    , case 
      when to_char(timestamp_col, 'hh24') between '08' and '20' 
      then 'work' 
      else 'other' 
     end 

trunc(timestamp_col, 'MM') - 给你一个月

case 
      when to_char(timestamp_col, 'hh24') between '08' and '20' 
      then 'work' 
      else 'other' 
     end 

- 让你无论是 '工作' 或 '其他'

+0

shurik,谢谢! – cma1kep