【问题标题】:column "location_time_slot.from_date" must appear in the GROUP BY clause or be used in an aggregate function列“location_time_slot.from_date”必须出现在 GROUP BY 子句中或在聚合函数中使用
【发布时间】:2021-06-07 09:09:30
【问题描述】:

这是我遇到问题的查询:

SELECT
    "fightEventId"
FROM
    location_time_slot
WHERE
    "fightId" IN (
        SELECT
            f.id
        FROM
            fights AS f
        WHERE
            f.status = 'CONFIRMED'
    )
AND "fightEventId" IN (
    SELECT
        fe.id
    FROM fight_events AS fe
    WHERE true AND (
        NOW() at time zone 'utc' >= fe.from AND NOW() at time zone 'utc' <= fe.to
    ) OR true AND NOW() at time zone 'utc' <= fe.to
      OR false AND NOW() at time zone 'utc' > fe.to
)
GROUP BY "fightEventId"
HAVING 
    COUNT("fightId") >= 2
ORDER BY from_date ASC ;

为什么会出现此错误?我想按location_time_slot 表中存在的from_date 字段值排序。

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    您将location_time_slot 的行按"fightEventId" 分组以应用HAVING 条件。当您进行分组时,关于from_date 的信息有些“丢失”(一组中所有行的from_dates 中的哪一个应该用于排序?)。

    因此,您还需要按from_date 分组(因此组中的所有行都将具有相同的from_date,这可用于对组进行排序)或在ORDER BY 中使用from_date聚合函数中的子句(如MAX(from_date);然后很清楚该组的哪个from_dates 用于对组进行排序)。

    通过一个属性对行组进行排序,这在组的所有行中不一定相同是根本不可能的。

    【讨论】:

    • 谢谢。让我看看。
    • 是的,一切都很清楚,谢谢你的提示。
    猜你喜欢
    • 2018-05-16
    • 2013-11-05
    • 2021-06-25
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多