Search code examples
sqlhana

How to group by hour in HANA


I have the following table in HANA :

vehicle_id    time                          roaming_time        parking_time
    1          Sep 01,2016 3:09:03 AM            3                   9
    2          Sep 01,2016 3:12:03 AM            6                   8
    1          Sep 01,2016 9:10:03 AM            10                  6
    4          Sep 01,2016 10:09:03 AM           9                   3
    1          Sep 01,2016 10:10:03 AM           10                  10
    4          Sep 01,2016 12:09:03 AM           3                   9

from these information I wanted to know that what is the sum of roaming_time and sum of parking_time for each hour from all the vehicles and want the output in the format:

time                         roaming_time     parking_time
____                         _____________    ____________
2016-09-01 00:00:00                3                9
2016-09-01 01:00:00                6                8
2016-09-01 02:00:00                9                6
2016-09-01 03:00:00                3                6
2016-09-01 04:00:00                12               3
2016-09-01 05:00:00                15               8
2016-09-01 06:00:00                18               4
2016-09-01 07:00:00                8                3
2016-09-01 08:00:00                9                4
2016-09-01 09:00:00                6                6
2016-09-01 10:00:00                6                9
........
2016-09-01 23:00:00                3                12

I need to group the following query which gives all the sum by hour wise and get the expected result:

select sum(roaming_time) as roaming_time,sum(parking_time) as parking_time 
          from time>='2016-09-01 00:00:00'
               time>='2016-09-01 23:59:59'

I do not know how to do the grouping by hour in HANA. Any help is appreciated


Solution

  • Here is one method . . . it converts the time to a date and hour format:

    select to_varchar(time, 'YYYY-MM-DD'), hour(time),
           sum(roaming_time) as roaming_time, sum(parking_time) as parking_time from t
    group by date(time), hour(time)
    order by to_varchar(time, 'YYYY-MM-DD'), hour(time);