Hive: Sum over a specified group (HiveQL)

后端 未结 6 836
渐次进展
渐次进展 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 14:55

    The table above looked like

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

    The user wanted a tabel with the total costs like the following

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

    Therefor we used the following query

    SELECT key, product_code,
    SUM(costs) OVER (PARTITION BY key ORDER BY key ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
    FROM test;
    

    So far so good. I want a column more, counting the occurences of each country

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

    Therefor I used the following query

    SELECT key, product_code,
    SUM(costs) OVER (PARTITION BY key ORDER BY key ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as total_costs
    COUNT(product code) OVER (PARTITION BY key ORDER BY key ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as occurences
    FROM test;
    

    Sadly this is not working. I get an cryptic error. To exclude an error in my query I want to ask if I did something wrong. Thanks

提交回复
热议问题