我想运行这个查询:

SELECT DISTINCT ON (address_id) purchases.address_id, purchases.*
FROM purchases
WHERE purchases.product_id = 1
ORDER BY purchases.purchased_at DESC

但是我得到了这个错误:

SELECT DISTINCT ON表达式必须匹配表达式的初始顺序

添加address_id作为第一个ORDER BY表达式可以消除错误,但我真的不想在address_id上添加排序。有没有可能不按address_id排序?


当前回答

对于任何使用Flask-SQLAlchemy的人来说,这对我来说都是有效的

from app import db
from app.models import Purchases
from sqlalchemy.orm import aliased
from sqlalchemy import desc

stmt = Purchases.query.distinct(Purchases.address_id).subquery('purchases')
alias = aliased(Purchases, stmt)
distinct = db.session.query(alias)
distinct.order_by(desc(alias.purchased_at))

其他回答

Window函数可以一次性解决这个问题:

SELECT DISTINCT ON (address_id) 
   LAST_VALUE(purchases.address_id) OVER wnd AS address_id
FROM "purchases"
WHERE "purchases"."product_id" = 1
WINDOW wnd AS (
   PARTITION BY address_id ORDER BY purchases.purchased_at DESC
   ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

你可以在子查询中根据address_id排序,然后在外部查询中根据你想要的排序。

SELECT * FROM 
    (SELECT DISTINCT ON (address_id) purchases.address_id, purchases.* 
    FROM "purchases" 
    WHERE "purchases"."product_id" = 1 ORDER BY address_id DESC ) 
ORDER BY purchased_at DESC

文档中说:

DISTINCT ON(表达式[,…])只保留给定表达式求值为相等的每一组行的第一行。[…注意,每个集合的“第一行”是不可预测的,除非使用ORDER BY来确保所需的行首先出现。[…DISTINCT ON表达式必须匹配最左边的ORDER BY表达式。

官方文档

因此,您必须将address_id添加到order中。

或者,如果你正在寻找包含每个address_id的最近购买的产品的完整行,并且结果按purchased_at排序,那么你正在尝试解决每组最大N的问题,可以通过以下方法解决:

适用于大多数dbms的通用解决方案:

SELECT t1.* FROM purchases t1
JOIN (
    SELECT address_id, max(purchased_at) max_purchased_at
    FROM purchases
    WHERE product_id = 1
    GROUP BY address_id
) t2
ON t1.address_id = t2.address_id AND t1.purchased_at = t2.max_purchased_at
ORDER BY t1.purchased_at DESC

基于@hkf的回答,一个更面向postgresql的解决方案:

SELECT * FROM (
  SELECT DISTINCT ON (address_id) *
  FROM purchases 
  WHERE product_id = 1
  ORDER BY address_id, purchased_at DESC
) t
ORDER BY purchased_at DESC

此处澄清、扩展和解决的问题:按某列排序并在另一列上不同地选择行

它也可以使用以下查询和其他答案来解决。

WITH purchase_data AS (
        SELECT address_id, purchased_at, product_id,
                row_number() OVER (PARTITION BY address_id ORDER BY purchased_at DESC) AS row_number
        FROM purchases
        WHERE product_id = 1)
SELECT address_id, purchased_at, product_id
FROM purchase_data where row_number = 1

你也可以通过使用group by子句来做到这一点

   SELECT purchases.address_id, purchases.* FROM "purchases"
    WHERE "purchases"."product_id" = 1 GROUP BY address_id,
purchases.purchased_at ORDER purchases.purchased_at DESC