我试图在 Postgres 中提交一个只返回不同元组的查询。在我的示例查询中,我不希望一个cluster_id/feed_id 组合存在多次的重复条目。如果我做一个简单的:
select distinct on (cluster_info.cluster_id, feed_id)
cluster_info.cluster_id, num_docs, feed_id, url_time
from url_info
join cluster_info on (cluster_info.cluster_id = url_info.cluster_id)
where feed_id in (select pot_seeder from potentials)
and num_docs > 5 and url_time > '2012-04-16';
我明白了,但我也想根据num_docs
. 因此,当我执行以下操作时:
select distinct on (cluster_info.cluster_id, feed_id)
cluster_info.cluster_id, num_docs, feed_id, url_time
from url_info join cluster_info
on (cluster_info.cluster_id = url_info.cluster_id)
where feed_id in (select pot_seeder from potentials)
and num_docs > 5 and url_time > '2012-04-16'
order by num_docs desc;
我收到以下错误:
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
LINE 1: select distinct on (cluster_info.cluster_id, feed_id) cluste...
我想我明白为什么我会收到错误(除非我以某种方式明确描述该组,否则不能按元组分组)但是我该怎么做呢?或者,如果我对错误的解释不正确,有没有办法实现我的初始目标?