hql将时间分割为时间间隔

nom7f22z  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(370)

我有一个包含一些数据的配置单元表,我想将它拆分为15分钟间隔,并返回每个间隔的总调用持续时间
配置单元表示例:

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

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

ohfgkhjo

ohfgkhjo1#

这是基本的解决办法。
显示的时间戳( 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 |
+----+---------------------+----------+

相关问题