mysql - 滞后函数中组函数的使用无效

标签 mysql sql date group-by window-functions

我是 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 without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'test.contacts.createdate'; this is incompatible with sql_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/

相关文章:

mysql - 如何恢复mysql数据库

php - mysql 选择按日期和时间排序 str_to_date

mysql - PhpMyAdmin 导出大小

sql - 在不依赖数据库名称的情况下导出 sql developer 上的数据

mysql - 如何在 MySQL 中返回数据透视表输出?

python - 我将如何在 GeoDjango 中编写此查询? (它是 Django 中用于地理计算的库)

mysql - 选择 mysql 中两个表的每月数据计数

java - mysql比较两个日期

grails - 如何初始化 Grails 域对象中的日期字段以充当时间戳?

mysql - mysql 上的触发更新第二次运行