如何在HANA中截断日期

时间:2017-02-07 10:19:21

标签: database date sap hana

我有一个查询来获取每个日期行驶不到100公里的公交车的数量。所以我在PostgreSQL中使用查询

     select day,count(*)as bus_count from(

     SELECT date_trunc('hour',start_time)::timestamp::date  as  day,bus_id,sum(distance_two_points) as distance
     FROM public.datatable where start_time >= '2015-09-05 00:00:00' and start_time <= '2015-09-05 23:59:59' 
     group by day,bus_id 

    ) as A where distance<=250000 group by day

查询返回结果

       day                bus_id   distance
       ___               ________  _________
    "2015-09-05 00:00:00"   1       523247
    "2015-09-05 00:00:00"   2       135114
    "2015-09-05 00:00:00"   3       178560
    "2015-09-05 00:00:00"   4       400071
    "2015-09-05 00:00:00"   5       312832
    "2015-09-05 00:00:00"   6       237075

所以我现在想在SAP HANA中使用相同的查询(实现相同的结果),但没有日期截断功能,我也尝试了

 SELECT EXTRACT (DAY FROM TO_DATE (START_TIME, 'YYYY-MM-DD')) "extract"  as  day,bus_id,sum(distance_two_points) as distance
         FROM public.datatable where start_time >= '2015-09-05 00:00:00' and start_time <= '2015-09-05 23:59:59' 
         group by day,bus_id 

        ) as A where distance<=250000 group by day

感谢任何帮助。

4 个答案:

答案 0 :(得分:0)

为什么不使用CAST()转换函数?

select 
    cast( now() as date ) myDate
from dummy;

答案 1 :(得分:0)

我更喜欢使用seconds_between()或nano100_between()函数。

select now(), 
       add_seconds(  to_date('1970.01.01', 'YYYY.MM.DD'),
                     round(
                        SECONDS_BETWEEN(
                            to_date('1970.01.01', 'YYYY.MM.DD'),
                            now()
                        )/3600
                     )*3600
       ) 
from dummy;

这看起来有点难看,但是考虑到to_date()仅计算一次而不是针对每一行计算,并且秒算术接近Hana在内部存储值的方式,因此应该是最有效的方法。 也是最灵活的,可以按秒,分钟,小时,天……一整天。一年以下的一切都很好。

PS:round()支持所有舍入和截断选项。

答案 2 :(得分:-1)

假设您的start_time属于某种数据/时间类型(例如SECONDDATE),您可以使用

...TO_NVARCHAR(START_TIME, 'YYYY-MM-DD') AS DAY...

而不是PostgreSQL中的date_trunc ...

答案 3 :(得分:-1)

转换为非日期时间数据类型通常不是一个好主意(额外的解析,编码,语义......)。

而是使用不太精细的日期时间数据类型:在这种情况下为daydate

create column table datatab (start_time seconddate, bus_id int, distance_two_points decimal (10, 2));


insert into datatab values (to_seconddate('05.09.2015 13:12:00'), 1, 50.2);    
insert into datatab values (to_seconddate('05.09.2015 13:22:00'), 1, 1.2);    
insert into datatab values (to_seconddate('05.09.2015 15:32:00'), 1, 24);    

insert into datatab values (to_seconddate('05.09.2015 13:12:00'), 1, 50.2);    

insert into datatab values (to_seconddate('05.09.2015 14:22:00'), 2, 1.2);    
insert into datatab values (to_seconddate('05.09.2015 16:32:00'), 2, 24);    


  select to_seconddate(day) as day,count(*) as bus_count from(

     SELECT to_date(start_time) as day, bus_id, sum(distance_two_points) as distance
     FROM  datatab
     where start_time between '2015-09-05 00:00:00' and '2015-09-05 23:59:59' 
     group by to_date(start_time),bus_id 

    ) as A 
    where distance<=250000 
    group by day;

内部查询为您提供:

DAY         BUS_ID  DISTANCE
2015-09-05  1       75.40   
2015-09-05  2       25.20   

所以,你的seconddate&#34; start_time&#34;现在汇总为daydate,然后转换回&#39; seconddate&#39;。