错误:SELECT列表表达式引用了created_utc列,该列既未在[2:49]进行分组也未聚合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误:SELECT列表表达式引用了created_utc列,该列既未在[2:49]进行分组也未聚合相关的知识,希望对你有一定的参考价值。
我有一张桌子:t
我的目标:仅提取表中得分最高的“id”,并将其按week_number分组。
查询:
SELECT id,
CAST(EXTRACT(WEEK FROM TIMESTAMP_SECONDS(created_utc)) AS STRING) AS week_number,
MAX(score) AS highest_score
FROM t
WHERE body='r/twinpeaks'
GROUP BY id;
我收到此错误:错误:SELECT列表表达引用列created_utc既未分组也未汇总在[2:49]
我尝试这样做:
SELECT id,
CAST(EXTRACT(WEEK FROM TIMESTAMP_SECONDS(created_utc)) AS STRING) AS week_number,
MAX(score) AS highest_score
FROM t
WHERE body='r/twinpeaks'
GROUP BY week_number, id;
但这就是我得到的:
Row id week_number highest_score
1 dmkb6sv 36 1
2 dn1cd2s 37 2
3 dn43h1k 38 16
4 dn3xf18 38 1
5 dn7i1ko 38 1
6 dnpr9b1 39 1
我要这个:
Row id week_number highest_score
1 dmkb6sv 36 1
2 dn1cd2s 37 2
3 dn43h1k 38 16
6 dnpr9b1 39 1
答案
你可以尝试在这里使用ROW_NUMBER
:
SELECT *
FROM
(
SELECT id,
CAST(EXTRACT(WEEK FROM TIMESTAMP_SECONDS(created_utc)) AS STRING) AS week_number,
score,
ROW_NUMBER() OVER (PARTITION BY
CAST(EXTRACT(WEEK FROM TIMESTAMP_SECONDS(created_utc)) AS STRING)
ORDER BY score DESC) rn
FROM t
WHERE body = 'r/twinpeaks'
) t
WHERE rn = 1;
这将返回每个相应周数的最高分的记录。我在这里假设你要么不关心第一关系,要么不关系。如果您需要处理关系,则可以使用排名函数代替行号。
另一答案
以下是BigQuery Standard SQL
#standardSQL
SELECT
EXTRACT(WEEK FROM TIMESTAMP_SECONDS(created_utc)) AS week_number,
ARRAY_AGG(id ORDER BY score DESC LIMIT 1)[OFFSET(0)] id,
ARRAY_AGG(score ORDER BY score DESC LIMIT 1)[OFFSET(0)] highest_score
FROM `project.dataset.table`
WHERE body = 'r/twinpeaks'
GROUP BY week_number
ORDER BY week_number
以上是关于错误:SELECT列表表达式引用了created_utc列,该列既未在[2:49]进行分组也未聚合的主要内容,如果未能解决你的问题,请参考以下文章
错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法
如何修复 BigQuery 中的错误“选择列表表达式 [...] 既不分组也不聚合的引用”?
未捕获的错误:语法错误,无法识别的表达式:select:[id * =“devices_0_command”] [关闭]
如何解决 C# 中 linq 的 lambda 表达式中的对象引用错误?
SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT 列表的表达式 #3 不在 GROUP BY 子句中并且包含非聚合