sql—在带有两个select列的postgres(红移)中运行最大聚合查询时出现的问题

clj7thdc  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(221)

我正在尝试对红移表运行此简单查询:

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\u monitoring.created\u date”必须出现在group by子句中或在聚合函数中使用;
但是如果不选择 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的问题?

jv4diomz

jv4diomz1#

您的查询也不应该在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;

这应该可以在任何一个数据库中使用。

相关问题