2017-08-31 98 views
0

我有一些數據的蜂房表和我想它在分割爲15個分鐘間隔等返回的總呼叫持續時間的每個時間間隔HQL分割時間爲間隔

蜂巢表例如:

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 

是什麼做的,在一個有效的方式最好的解決辦法?

謝謝。

+0

不使用TIMESTAMP數據類型和時間戳ISO格式的原因嗎? –

+0

其實開始日期和結束日期是以時間戳格式存儲的。 – soupe

+0

那麼,爲什麼你在這裏發佈'09/08/2017-18:31:01'而不是'2017-08-09 18:31:01'? –

回答

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 | 
+----+---------------------+----------+ 
+0

謝謝。它工作得很好。 – soupe