我想運行此查詢:PostgreSQL的DISTINCT與不同的ORDER BY
SELECT DISTINCT ON (address_id) purchases.address_id, purchases.*
FROM purchases
WHERE purchases.product_id = 1
ORDER BY purchases.purchased_at DESC
但我得到這個錯誤:
PG::Error: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
添加address_id
爲第一ORDER BY
表達沉默的錯誤,但我真的不想在address_id
上添加排序。沒有address_id
訂購可以嗎?
您的訂單子句purchased_at不address_id.Can你讓你的問題清楚。 – Teja 2012-03-20 22:01:46
我的訂單有購買,因爲我想要它,但postgres還要求地址(請參閱錯誤消息)。 – 2012-03-20 22:03:50
完全解答在這裏 - http://stackoverflow.com/questions/9796078/selecting-rows-ordered-by-some-column-and-disctincton-another 感謝http://stackoverflow.com/users/ 268273/mosty-mostacho – 2012-12-21 23:40:39