2017-09-27 104 views
0

我有一個叫做計劃表,有以下欄目:HIVE:查找運行總計

 
ProgDate(Date) 
Episode(String) 
Impression_id(int) 
ProgName(String) 

我想找出每個日期和事件總的印象,對此我有以下查詢這是工作的罰款

Select progdate, episode, count(distinct impression_id) Impression from Program where progname='BBC' group by progdate, episode order by progdate, episode; 
 
Result: 
ProgDate  Episode  Impression  
20160919  1  5   
20160920  1  15   
20160921  1  10   
20160922  1  5   
20160923  2  25   
20160924  2  10   
20160925  2  25   

但我也想找出每一集的累計。我嘗試搜索如何查找總跑位數,但它將所有以前的總數加起來。 OVER()

 
Date  Episode  Impression CumulativeImpressionsPerChannel  
20160919  1  5    5 
20160920  1  15    20 
20160921  1  10    30 
20160922  1  5    35 
20160923  2  25    25 
20160924  2  10    35 
20160925  2  25    60 

回答

0

最近的蜂巢HQL支持版本的窗口分析功能(ref 1)(ref 2),包括SUM()假設你有這樣的版本:我想每集運行總計,如下圖所示我曾經模仿使用PostgreSQL語法在SQL Fiddle

CREATE TABLE d 
    (ProgDate int, Episode int, Impression int) 
; 

INSERT INTO d 
    (ProgDate, Episode, Impression) 
VALUES 
    (20160919, 1, 5), 
    (20160920, 1, 15), 
    (20160921, 1, 10), 
    (20160922, 1, 5), 
    (20160923, 2, 25), 
    (20160924, 2, 10), 
    (20160925, 2, 25) 
; 

查詢1

select 
     ProgDate, Episode, Impression 
    , sum(Impression) over(partition by Episode order by ProgDate) CumImpsPerChannel 
    , sum(Impression) over(order by ProgDate) CumOverall 
from (
     Select progdate, episode, count(distinct impression_id) Impression 
     from Program 
     where progname='BBC' 
     group by progdate, episode order by progdate, episode 
    ) d 

Results

| progdate | episode | impression | cumimpsperchannel | 
|----------|---------|------------|-------------------| 
| 20160919 |  1 |   5 |     5 | 
| 20160920 |  1 |   15 |    20 | 
| 20160921 |  1 |   10 |    30 | 
| 20160922 |  1 |   5 |    35 | 
| 20160923 |  2 |   25 |    25 | 
| 20160924 |  2 |   10 |    35 | 
| 20160925 |  2 |   25 |    60 | 
+0

謝謝!你的回答完美地解決了我的問題還有一個問題,如何獲得完整的累計跑步總數,不管日期或情節如下: – Huzefa

+0

'| | progdate |情節|印象| cumimpsperchannel | | ---------- | --------- | ------------ | -------------- ----- | | 20160919 | 1 | 5 | 5 | | 20160920 | 1 | 15 | 20 | | 20160921 | 1 | 10 | 30 | | 20160922 | 1 | 5 | 35 | | 20160923 | 2 | 25 | 60 | | 20160924 | 2 | 10 | 70 | | 20160925 | 2 | 25 | 95 |' – Huzefa

+0

只需在OVER()子句中刪除'partition by ....'部分,就已經添加了這個以上的查詢 –