如何在HANA中按小时分组

时间:2017-02-22 01:27:27

标签: sql sap hana sql-scripts

我在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

根据这些信息,我想知道所有车辆每小时的漫游时间和停车时间总和是多少,并希望以下列格式输出:

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

我需要对以下查询进行分组,该查询以小时为单位给出所有总和并获得预期结果:

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'

我不知道如何在HANA中按小时进行分组。任何帮助表示赞赏

3 个答案:

答案 0 :(得分:2)

这是一种方法。 。 。它将时间转换为日期和小时格式:

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);

答案 1 :(得分:1)

SERIES_ROUND()使用date()子句。避免在大型数据集上使用hour()select SERIES_ROUND(time, 'INTERVAL 1 HOUR') as time, sum(roaming_time) as roaming_time, sum(parking_time) as parking_time from t group by SERIES_ROUND(time, 'INTERVAL 1 HOUR') order by SERIES_ROUND(time, 'INTERVAL 1 HOUR'); 以及类似的数据/时间函数,因为它们往往较慢。

Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("C:\myprog.exe", null, null, intProcessID)

答案 2 :(得分:0)

另一种方法是将其转换为字符串,尤其是在不需要进一步计算时间的情况下。 这看起来像这样:

select to_varchar(time, 'DD.MM.YYYY HH24') as parking_hour ,
       sum(roaming_time) as roaming_time, sum(parking_time) as parking_time from t
group by to_varchar(time, 'DD.MM.YYYY HH24') as parking_hour
order byto_varchar(time, 'DD.MM.YYYY HH24') as parking_hour;