2013-08-23 69 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