我试图编写以下查询with 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
Run Code Online (Sandbox Code Playgroud)
我需要获取所有行,按照我在GROUP BY子句中指定的那些进行分组max date.但我得到了错误
ERROR: column "account.date" must appear in
the GROUP BY clause or be used in an aggregate function
Run Code Online (Sandbox Code Playgroud)
我在聚合函数中使用它.怎么了?
您可以使用窗口函数来指定行号(如果要取最后绑定的行,则可以使用排名):
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |