我是 SQL 新手,有一个相当简单的查询,但在尝试使用它时,我不断收到错误“无效使用组函数”。这是我的查询:
select CreateDate as date,
count(*) as count,
lag(count(*), 1) OVER (order by CreateDate) as Previous
from contacts
有人可以解释为什么这不起作用以及如何让它正常运行吗?
最佳答案
您的查询引发错误消息:
ER_MIX_OF_GROUP_FUNC_AND_FIELDS
: In aggregated query withoutGROUP BY
, expression #1 ofSELECT
list contains nonaggregated column 'test.contacts.createdate'; this is incompatible withsql_mode=only_full_group_by
这与lag()
无关。您的查询只是缺少 group by
子句:
select
CreateDate as date,
count(*) as count,
lag(count(*), 1) over(order by CreateDate) as previous
from contacts
group by createdate
关于mysql - 滞后函数中组函数的使用无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61281172/