distinct-on

PostgreSQL DISTINCT ON with different ORDER BY

被刻印的时光 ゝ 提交于 2019-11-26 01:06:50
问题 I want to run this query: SELECT DISTINCT ON (address_id) purchases.address_id, purchases.* FROM purchases WHERE purchases.product_id = 1 ORDER BY purchases.purchased_at DESC But I get this error: PG::Error: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions Adding address_id as first ORDER BY expression silences the error, but I really don\'t want to add sorting over address_id . Is it possible to do without ordering by address_id ? 回答1: Documentation says: