我尝试用HAVING
子句编写以下查询,以避免编写子查询,从而使查询的可读性稍差。:
SELECT partner_id, player_id, currency_id, platform_id
FROM account
GROUP BY partner_id, player_id, currency_id, platform_id
HAVING MAX(date) = date
我需要获取所有行,按照我在GROUP BY
子句中指定的具有max date
的行进行分组。但是我得到了关于
ERROR: column "account.date" must appear in
the GROUP BY clause or be used in an aggregate function
我在聚合函数中使用它。怎么了?
答案 0 :(得分:5)
您可以使用window function分配行号(如果您想要排列最上面的行,则使用排名):
SELECT * FROM (
SELECT partner_id, player_id, currency_id, platform_id,
ROW_NUMBER() OVER (
PARTITION BY partner_id, player_id, currency_id, platform_id
ORDER BY date desc) as rn
FROM account
) as t
WHERE t.rn = 1
答案 1 :(得分:0)
尝试以下查询:
SELECT partner_id, player_id, currency_id, platform_id
FROM account
GROUP BY date, partner_id, player_id, currency_id, platform_id
HAVING MAX(date) = date
这里你忘了在group by子句中添加日期(列名)。
请告诉我,这对你有用吗?