同时使用最大和总和

时间:2018-08-29 16:33:22

标签: hadoop hive cloudera

我遇到一种情况,我必须求和最大数量才能处理多个     每天的条目。     我在 cloudera hive 中有一个输入表:

    ----------------------------
    date1    |  date2    |  qty
    ----------------------------
    20180101 | 20180101 |   50
    ----------------------------
    20180101 | 20180101  |  15
    ----------------------------
    20180101 | 20180102  | 1
    ----------------------------
    20180101 | 20180103  | 3
    ----------------------------
    20180101 | 20180104  | 2
    ----------------------------
Final answer I need is :

    ----------------------------
    date1      |    qty
     ----------------------------
    20180101   |    56
    ----------------------------

Logic: For date1 cal 20180101   
= max(20180101 i.e date2) + sum ( all other date2)   
= max(50,15) + sum(1,3,2) 
= 56

**Will the lag / lead work here, can anyone help me with a hint of the 
solution? 

Thanks in advance.** 

1 个答案:

答案 0 :(得分:1)

您可以尝试以下查询并检查

select
date1,
Sum(qty_new)
from
(select date1, date2, max(qty) qty_new from 
table group by date1, date2) a
group by date1