【问题标题】:Is there a way of dividing a column by the column total in presto?有没有办法将列除以列总数?
【发布时间】:2019-04-16 03:44:38
【问题描述】:

我正在 presto 中设置查询。

我有两列——一列是类别,另一列是数字。

我想通过总数来设置数字列,同时将不同的类别组合在一起

something like this is what I have in mind

我很确定如何使用“with”结构来做到这一点,但是有没有办法在同一张表中做到这一点?

【问题讨论】:

    标签: presto


    【解决方案1】:

    您可以为此使用window functions

    WITH data (category, number) AS (
        VALUES
            ('A', 1),
            ('A', 2),
            ('A', 3),
            ('B', 4),
            ('B', 5),
            ('B', 6)
    )
    SELECT category, number * 1e0 / sum(number) OVER (PARTITION BY category)
    FROM data
    

    产生:

     category |        _col1
    ----------+---------------------
     A        | 0.16666666666666666
     A        |  0.3333333333333333
     A        |                 0.5
     B        | 0.26666666666666666
     B        |  0.3333333333333333
     B        |                 0.4
    (6 rows)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-07
      • 2016-09-06
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      相关资源
      最近更新 更多