hql将时间分成间隔

时间:2017-08-31 14:32:06

标签: hive hiveql

我有一个带有一些数据的Hive表,我想把它分成15分钟的间隔,然后返回每个间隔的总呼叫持续时间

Hive Table示例:

ID       Start                 End                Total Duration    
1      1502296261           1502325061                28800

我的输出应显示为:

ID       Interval             Duration    
1   2017-08-09 18:30:00        839
1   2017-08-09 18:45:00        900
1   2017-08-09 19:00:00        900
...
1   2017-08-10 02:15:00        900
1   2017-08-10 02:30:00        61

以有效的方式做到这一点的最佳解决方案是什么?

感谢。

1 个答案:

答案 0 :(得分:1)

这是基本的解决方案 显示的时间戳(Interval)取决于您的系统时区。

with t as (select stack(1,1,1502296261,1502325061) as (`ID`,`Start`,`End`))

select  t.`ID`                                                      as `ID`
       ,from_unixtime((t.`Start` div (15*60) + pe.pos)*(15*60))     as `Interval`
       ,    case 
                when    pe.pos = t.`End` div (15*60) - t.`Start` div (15*60) 
                then    t.`End`
                else    (t.`Start` div (15*60) + pe.pos + 1)*(15*60)
            end
        -   case 
                when    pe.pos = 0
                then    t.`Start`
                else    (t.`Start` div (15*60) + pe.pos)*(15*60)
            end                                                     as `Duration`

from    t
        lateral view 
            posexplode(split(space(int(t.`End` div (15*60) - t.`Start` div (15*60))),' ')) pe
;
+----+---------------------+----------+
| id |      interval       | duration |
+----+---------------------+----------+
|  1 | 2017-08-09 09:30:00 |      839 |
|  1 | 2017-08-09 09:45:00 |      900 |
|  1 | 2017-08-09 10:00:00 |      900 |
|  1 | 2017-08-09 10:15:00 |      900 |
|  1 | 2017-08-09 10:30:00 |      900 |
|  1 | 2017-08-09 10:45:00 |      900 |
|  1 | 2017-08-09 11:00:00 |      900 |
|  1 | 2017-08-09 11:15:00 |      900 |
|  1 | 2017-08-09 11:30:00 |      900 |
|  1 | 2017-08-09 11:45:00 |      900 |
|  1 | 2017-08-09 12:00:00 |      900 |
|  1 | 2017-08-09 12:15:00 |      900 |
|  1 | 2017-08-09 12:30:00 |      900 |
|  1 | 2017-08-09 12:45:00 |      900 |
|  1 | 2017-08-09 13:00:00 |      900 |
|  1 | 2017-08-09 13:15:00 |      900 |
|  1 | 2017-08-09 13:30:00 |      900 |
|  1 | 2017-08-09 13:45:00 |      900 |
|  1 | 2017-08-09 14:00:00 |      900 |
|  1 | 2017-08-09 14:15:00 |      900 |
|  1 | 2017-08-09 14:30:00 |      900 |
|  1 | 2017-08-09 14:45:00 |      900 |
|  1 | 2017-08-09 15:00:00 |      900 |
|  1 | 2017-08-09 15:15:00 |      900 |
|  1 | 2017-08-09 15:30:00 |      900 |
|  1 | 2017-08-09 15:45:00 |      900 |
|  1 | 2017-08-09 16:00:00 |      900 |
|  1 | 2017-08-09 16:15:00 |      900 |
|  1 | 2017-08-09 16:30:00 |      900 |
|  1 | 2017-08-09 16:45:00 |      900 |
|  1 | 2017-08-09 17:00:00 |      900 |
|  1 | 2017-08-09 17:15:00 |      900 |
|  1 | 2017-08-09 17:30:00 |       61 |
+----+---------------------+----------+