【问题标题】:how to take the standard deviation and sum of a count(*) result column如何获取计数(*)结果列的标准偏差和总和
【发布时间】:2021-09-27 06:20:26
【问题描述】:

我有一张这样的桌子:

firstname  lastname   values
john       doe        first
john       doe        second
john       doe        third
joe        public     first
joe        public     second
robert     willians   first
robert     willians   second

我想计算每个名字、姓氏对出现的次数。然后计算计数列的总和、平均值和标准差。

目前,我有:

select stddev(t.count(*)), avg(t.count(*)), sum(t.count(*))
   from (select count(*), firstname, lastname
      from table
      group by firstname, lastname) t

这给了

未知的用户定义函数 t."count()",t."count()",t."count(*)"

【问题讨论】:

    标签: mysql sql snowflake-cloud-data-platform


    【解决方案1】:

    只需使用更有意义的列别名:

    select stddev(t.cnt), avg(t.cnt), sum(t.cnt)
    from (select count(*) as cnt, firstname, lastname
          from table
          group by firstname, lastname
         ) t
    

    【讨论】:

      【解决方案2】:

      您需要为COUNT(*) 列分配一个别名

      select stddev(t.count), avg(t.count), sum(t.count)
      from (select count(*) AS count, firstname, lastname
            from table
            group by firstname, lastname) t
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多