我的查询以:
结尾GROUP BY supervisor,
agent,
task
ORDER BY supervisor,
agent,
'task_count'
我遇到的是每个代理可以拥有超过100个任务,而且我只希望列出每个代理的前20个(通过task_count)。如果我使用LIMIT,则会将整个结果列表限制为20.是否有办法只将LIMIT应用于代理列?谢谢!
答案 0 :(得分:2)
不,你可以使用row_number()
:
select . . .
from (select t.*,
row_number() over (partition by agent order by task_count desc) as seqnum
from t
) t
where seqnum <= 20;
如果正在计算task_count
,请执行以下操作:
select t.*
from (select t.agent, t.task, count(*) as task_count,
row_number() over (partition by agent order by count(*) desc) as seqnum
from t
group by t.agent, t.task
) t
where seqnum <= 20;
答案 1 :(得分:0)
不确定您的平台,但在DB2上它看起来像这样:
select -- stuff
supervisor, agent, ordn
from (
SELECT -- stuff
supervisor, agent,
row_number() over (partition by supervisor, agent order by count(*) RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as ordn
GROUP BY supervisor, agent
) x
where ordn <= 20
order by supervisor, agent, ordn
请注意,您需要范围,因为默认情况下,范围是当前的和无限制的。