Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在具有两个select列的Postgres(红移)中运行最大聚合查询时出现问题_Sql_Postgresql_Amazon Redshift - Fatal编程技术网

Sql 在具有两个select列的Postgres(红移)中运行最大聚合查询时出现问题

Sql 在具有两个select列的Postgres(红移)中运行最大聚合查询时出现问题,sql,postgresql,amazon-redshift,Sql,Postgresql,Amazon 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') 这是一个与Postgres相关的问题还是特定于红移的问题?您的查询也不应该在MySQL中工作,因为您有一个聚合查询,没有group by和未聚

我试图在红移表上运行此简单查询:

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')

这是一个与Postgres相关的问题还是特定于红移的问题?

您的查询也不应该在MySQL中工作,因为您有一个聚合查询,没有
group by
和未聚合的列。查询的格式不正确

相反,请使用
按顺序
限制

select id, created_date
from records.access_monitoring
where created_date < '2020-05-19 16:00:00'
order by id desc
limit 1;
选择id,创建日期
从records.access\u监控
创建日期<'2020-05-19 16:00:00'
按id描述订购
限值1;

这两个数据库都可以使用。

非常感谢。事实上,我在MySQL(Aurora)上运行了它,它确实起了作用。这是有道理的,不应该这样做,但确实如此。非常感谢。为了确保,ORDERBY子句应该是
id
,对吗?既然我想要max(id)?@madu。较旧版本的MySQL允许这种语法,返回任意的
创建日期
。是的。我误解了逻辑。
orderby
应为by
id
select id, created_date
from records.access_monitoring
where created_date < '2020-05-19 16:00:00'
order by id desc
limit 1;