我想获得过去一周每天的结果数量。不幸的是,我在查询中遇到了这个错误:
An expression starting with "APP_ID" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified.. SQLCODE=-119, SQLSTATE=42803, DRIVER=3.67.27
查询:
SELECT DAYNAME(created), app_id FROM Annotation WHERE app_id = 1 AND (created < CURRENT DATE - 7 DAYS) GROUP BY DAYNAME(created) ORDER BY created
问题与GROUP BY
声明有关。这有什么问题?
答案 0 :(得分:1)
我认为错误非常明确 - appid
位于SELECT
但不是GROUP BY
。解决方案是您需要一个聚合函数。我希望这样的事情:
SELECT DAYNAME(created), COUNT(*)
FROM Annotation a
WHERE app_id = 1 AND (created < CURRENT DATE - 7 DAYS)
GROUP BY DAYNAME(created)
ORDER BY MIN(created);
答案 1 :(得分:0)
如果您想使用分组依据,那么每个列都必须在您的group by语句中,或者汇总。
select col1, col2, 'same-for-every-row', sum(col3) as col3_sum, avg(col4) as col4_avg
from schema.table
group by col1, col2
这是有效的,因为col1和col2已被分组,但每隔一列都有一些聚合,以了解如何对所有值进行分组。
您当前的语句不起作用,因为虽然您按日期分组,但您尚未指定如何对app_id的所有行进行分组,您需要指定它们应通过求和或平均或查找进行分组最小化或以其他方式聚合,该组中的所有值。
例外是使用字符串创建的列,“每个行相同”,这不需要聚合,因为它每次都是相同的。