【发布时间】:2018-12-23 12:09:27
【问题描述】:
是否可以限制窗口函数的结果,有分区,没有子查询?此代码位于 postgres/mysql 中。我正在寻找 mysql 和 postgres 中的解决方案。
例如:假设连接与问题的重点无关。
select acct.name, we.channel, count(*) as cnt,
max(count(*)) over (partition by name order by count(*) desc) as max_cnt
from web_events we join accounts acct
on we.account_id=acct.id
group by acct.name, we.channel
order by name, max_cnt desc;
这个查询的结果是:
我只想显示每个窗口分区的第一行。 例如:带有cnt的行:[3M,19],[Abbott Labortories,20]
我尝试了以下不起作用(在窗口函数中添加了限制 1):
select acct.name, we.channel, count(*) as cnt,
max(count(*)) over (partition by name order by count(*) desc limit 1) as max_cnt
from web_events we join accounts acct
on we.account_id=acct.id
group by acct.name, we.channel
order by name, max_cnt desc;
【问题讨论】:
标签: mysql sql postgresql window-functions partition