如何在一个查询中合并两个聚合函数?

时间:2019-08-05 07:20:55

标签: google-bigquery

我正在尝试分析每天的平均值(每个创建的内容共#条评论)。我有一个清单 1.评论数, 2.创建的内容数 天明智。但是我无法将它们全部输出到同一表的列中。

Google BigQuery。

问题是-

代码1:

当我将前一列的结果合并到下一列时,它会显示无法识别的名称 comment_per_UGC

代码2:

如果我使用avg(sum(comments)/count(distinct (postId))),则会出现错误消息,提示不允许使用聚合函数进行聚合

代码1-

select date_posted, count(*) as Commentors, sum(comments) as comment, 
count(distinct (postId)) as UGC, sum(comments)/count(distinct (postId)) 
as comment_per_UGC, avg(comment_per_UGC) as Overall_Avg_UGC

代码2-

select date_posted, count(*) as Commentors, sum(comments) as comment, 
count(distinct (postId)) as UGC, sum(comments)/count(distinct (postId)) 
as comment_per_UGC, avg(sum(comments)/count(distinct (postId))) as 
Overall_Avg_UGC

预期输出-平均(comment_per_UGC)作为最后一列(第6列)

1 个答案:

答案 0 :(得分:0)

尝试使用子查询;

    Select date_posted,avg(comment_per_UGC) from (    
    select date_posted, count(*) as Commentors, sum(comments) as comment, 
        count(distinct (postId)) as UGC, sum(comments)/count(distinct (postId)) 
        as comment_per_UGC


    ) t
group by date_posted