2016-04-21 14 views
0

einige werden sagen "eine andere Frage von diesem Typ", aber hier ist mein Problem. all dies funktioniert so entworfen:Orakel Gruppe nach Datum mit bestimmter Zeit

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) 

die Idee von Tagen mit einer „neuen Zeitbasis“ zu Gruppe ist und prüfen, ob Daten vor oder nach einer bestimmten Tageszeit sind. Aufgrund von Verträgen dauern die Tage in unserer Firma von 4.30 Uhr. an diesem Tag bis 4.30 Uhr. nächster Tag. Meine obige Lösung funktioniert (mit wenig Daten), aber ich denke, es gibt einen einfacheren Weg, um Ergebnisse zu erzielen. irgendeine Idee?

Antwort

0

Nicht sicher, was Sie genau versuchen, aber Sie scheinen in dieser Frage stecken geblieben zu sein ... vielleicht ist das die Lösung, nach der Sie suchen?

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

Dadurch wird der Zeitstempel (zeigt vielleicht „heutige“ Datum) erhalten, aber wenn die Zeit bis 04.30 Uhr, wird die leaddate date „gestern“ sein.

Wenn dies nicht das ist, was Sie gesucht haben, versuchen Sie bitte, Ihre Frage zu klären.