我正在尝试在Redshift表上运行这个简单的查询:
select
max(id),
created_date
from records.access_monitoring
where created_date < to_timestamp('2020-05-19 16:00:00', 'YYYY-MM-DD HH24:MI:SS')
这仅给出 '2020-05-19 16:00:00' 之前日期的最大 ID 值。 当我针对 MySQL 表运行它时,此查询工作正常,但是,我在 Redshift 中的同一表给出了此错误:
无效操作:必须显示"access_monitoring.创建日期"列 在 GROUP BY 子句中或在聚合函数中使用;
但是,如果在 Redshift 中运行查询而不选择created_date
则工作正常:
select
max(set_id)
from records.access_monitoring
where created_date < to_timestamp('2020-05-19 16:00:00', 'YYYY-MM-DD HH24:MI:SS')
这是与Postgres相关的问题还是特定于Redshift的问题?
您的查询也不应该在 MySQL 中工作,因为您有一个没有group by
和未聚合列的聚合查询。 查询格式不正确。
相反,请使用order by
和limit
:
select id, created_date
from records.access_monitoring
where created_date < '2020-05-19 16:00:00'
order by id desc
limit 1;
这应该适用于任一数据库。