Hive: Sum over a specified group (HiveQL)

后端 未结 6 831
渐次进展
渐次进展 2021-02-04 14:49

I have a table:

key    product_code    cost
1      UK              20
1      US              10
1      EU              5
2      UK              3
2      EU               


        
6条回答
  •  死守一世寂寞
    2021-02-04 15:03

    The analytics function sum gives cumulative sums. For example, if you did:

    select key, product_code, cost, sum(cost) over (partition by key) as total_costs from test
    

    then you would get:

    key    product_code    cost     total_costs
    1      UK              20       20
    1      US              10       30
    1      EU              5        35
    2      UK              3        3
    2      EU              6        9
    

    which, it seems, is not what you want.

    Instead, you should use the aggregation function sum, combined with a self join to accomplish this:

    select test.key, test.product_code, test.cost, agg.total_cost
    from (
      select key, sum(cost) as total_cost
      from test
      group by key
    ) agg
    join test
    on agg.key = test.key;
    

提交回复
热议问题