oracle group按日期与特定时间

时间:2016-04-21 12:43:09

标签: oracle group-by

有人会说“那个家伙的另一个问题”,但这是我的问题。所有这些都按设计工作:

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。明天。我上面的解决方案工作(数据很少),但我想有一种更简单的方法来获得结果。任何想法?

1 个答案:

答案 0 :(得分:0)

不确定你想要做什么,但你似乎仍然坚持这个问题......也许这是你正在寻找的解决方案?

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

这将保留时间戳(可能显示&#34;今天&#34;日期),但如果时间早于凌晨4:30,那么领先日期将是&#34;昨天&#39; s&# 34;日期。

如果这不是您想要的,请尝试澄清您的问题。

相关问题