同时使用max和sum

问题描述 投票:0回答:1

我有一种情况,我必须总和和最大数量来处理每天多个条目。我在cloudera配置单元中有一个输入表:

    ----------------------------
    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.** 
hadoop hive cloudera
1个回答
1
投票

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

select
date1,
Sum(qty_new)
from
(select date1, date2, max(qty) qty_new from 
table group by date1, date2) a
group by date1
© www.soinside.com 2019 - 2024. All rights reserved.