参数 * 只能用于 COUNT(*)

Argument * can only be used in COUNT(*)

我的 table 看起来像这样:

examID action   timeSinceStart
a     write     300
a     answer   2535
b     answer   3834
a     answer   2900
c     write     774
d     edit     3674
c     answer   3489
a     edit     3983
c     answer    300

我寻求以下输出:

examID averageTime
a      2717.5
b      3834
c      1894.5    

也就是说,我只看actionsanswer,我只写了一次examID,另一列是average time since start at the point of answering

是否可以直接这样,不用求和一列,计数一列,然后写平均公式?

我的“复杂”方法不起作用:

%%bigquery df_average_times
SELECT countTimes.examID, sumTimes.answerSum, countTimes.answerCount, (sumTimes/countTimes) as averageTime
FROM
 ( SELECT examID, timeSinceStart, action, SUM(*) as answerSum
 FROM `some-database.bq.results` 
 GROUP BY examID, timeSinceStart, action
 HAVING action='answer'
 ORDER BY examID) as sumTimes
 INNER JOIN
 ( SELECT examID, timeSinceStart, action, COUNT(*) as answerCount
 FROM `some-database.bq.results` 
 GROUP BY examID, timeSinceStart, action
 HAVING event='answer'
 ORDER BY examID) as countTimes
 ON falseAnswers.questionId = trueAnswers.questionId

错误状态:

ERROR:
 400 Argument * can only be used in COUNT(*) at [3:58]

我认为您需要以下查询 -

SELECT examID, AVG(timeSinceStart)
  FROM (SELECT examID, timeSinceStart
          FROM `some-database.bq.results`
         WHERE action='answer') X
 GROUP BY examID;

如果你想得到一个答案的平均时间,你可以使用:

SELECT examID, AVG(timeSinceStart)
FROM `some-database.bq.results`
WHERE action = 'answer' 
GROUP BY examID;

如果您想使这个有条件的——比如在同一查询中获取不同操作的平均值——您可以使用 CASE 表达式:

SELECT examID,
       AVG(CASE WHEN action = 'answer' THEN timeSinceStart END) avg_answer,
       AVG(CASE WHEN action = 'write' THEN timeSinceStart END) as avg_write
FROM `some-database.bq.results`
WHERE action = 'answer' 
GROUP BY examID;