2016-04-21 117 views
0

有些人會說「這個人的另一個問題」,但這是我的問題。這一切工作按設計:oracle group by date with specific time

with tab1 as ( 
select to_timestamp('04.02.15 14:25:21.503000000') as dt from dual union all 
select to_timestamp('04.02.15 14:25:25.154000000') as dt from dual union all 
select to_timestamp('09.02.15 22:20:36.861000000') as dt from dual union all 
select to_timestamp('09.02.15 22:20:36.883000000') as dt from dual union all 
select to_timestamp('10.02.15 04:19:13.839000000') as dt from dual union all 
select to_timestamp('10.02.15 04:13:18.142000000') as dt from dual union all 
select to_timestamp('10.02.15 12:43:18.171000000') as dt from dual union all 
select to_timestamp('11.02.15 04:30:53.654000000') as dt from dual union all 
select to_timestamp('11.02.15 22:00:38.951000000') as dt from dual union all 
select to_timestamp('11.02.15 22:00:42.014000000') as dt from dual union all 
select to_timestamp('16.02.15 08:50:43.967000000') as dt from dual union all 
select to_timestamp('16.02.15 16:35:41.387000000') as dt from dual union all 
select to_timestamp('16.02.15 16:35:42.835000000') as dt from dual union all 
select to_timestamp('17.02.15 04:21:08.542000000') as dt from dual union all 
select to_timestamp('17.02.15 04:21:08.912000000') as dt from dual union all 
select to_timestamp('17.02.15 04:06:09.818000000') as dt from dual union all 
select to_timestamp('17.02.15 04:40:39.411000000') as dt from dual union all 
select to_timestamp('18.02.15 04:41:08.218000000') as dt from dual union all 
select to_timestamp('18.02.15 03:20:40.609000000') as dt from dual union all 
select to_timestamp('18.02.15 01:20:40.712000000') as dt from dual union all 
select to_timestamp('20.02.15 06:55:42.185000000') as dt from dual union all 
select to_timestamp('20.02.15 12:55:42.364000000') as dt from dual union all 
select to_timestamp('20.02.15 12:55:42.518000000') as dt from dual union all 
select to_timestamp('20.02.15 12:55:43.874000000') as dt from dual union all 
select to_timestamp('20.02.15 14:16:05.080000000') as dt from dual union all 
select to_timestamp('20.02.15 18:14:17.630000000') as dt from dual union all 
select to_timestamp('22.02.15 21:25:40.683000000') as dt from dual union all 
select to_timestamp('22.02.15 21:25:42.046000000') as dt from dual union all 
select to_timestamp('23.02.15 12:43:27.246000000') as dt from dual 
order by dt 
), 
tab2 as( 
select trunc(dt) as leaddate, dt,  
case 
    when dt between (to_timestamp(trunc(dt)) + interval '04:30' hour to minute) and (to_timestamp(trunc(dt)) + interval '28:29' hour to minute) then (dt) 
    else (dt) - interval '04:30' hour to minute  
end as newBaseTime 
from tab1 
) 
select trunc(newBaseTime), 
sum(case when (dt <= to_timestamp(trunc(trunc(dt)),'dd.MM.yy') + interval '17:30' hour to minute) then 1 else 0 end) as beforeTS, 
    sum(case when (dt > to_timestamp(trunc(trunc(dt)),'dd.MM.yy') + interval '17:30' hour to minute) then 1 else 0 end) as afterTS 
from tab2 
group by trunc(newBaseTime) 
order by trunc(newBaseTime) 

的理念是通過集團天的「新時間基準」,並檢查日期是前或白天的定義之後。由於我們公司的合同日期從上午4點30分開始。這一天到了4.30。明天。我的解決方案上面的工作(數據很少),但我想有一個更簡單的方法來獲得結果。任何想法?

回答

0

不確定你正在嘗試做什麼,但你似乎陷入了這個問題......也許這是你正在尋找的解決方案?

select dt, trunc(dt - interval '270' minute) as leaddate from tab1 

這將保存時間戳(也許是顯示「今天」日期),但如果時間是凌晨4:30前,leaddate將是「昨天的」日期。

如果這不是你要找的,請嘗試澄清你的問題。