[英]How to limit Listagg results
I have a table with lots of employment info and I used listagg
to list all employments for each user, each user on a row instead of the max decode route.我有一个包含大量就业信息的表格,我使用
listagg
列出每个用户的所有就业,每个用户在一行上,而不是最大解码路线。 Using listagg
works but I want to restrict it to just the first 5 for example.使用
listagg
有效,但我想将其限制为仅前 5 个。 Some staff have over 30 employments listed.一些员工列出了 30 多个职位。
select distinct emp.id_Staff,
count (*) as cnt,
LISTAGG (emp.employer_name || '('|| emp.job_title || ')', ', ')
WITHIN GROUP (ORDER BY emp.employer_name) AS Employ_info
from (
SELECT distinct em.id_Staff, em.employer_name,
em.job_title, em_job_status
FROM employment em
where em.job_status = 'Active'
) emp
group by emp.id_Staff
You can use row_number()
to enumerate the values.您可以使用
row_number()
枚举值。 Then use a case
in listagg()
to just take the first five records:然后在
listagg()
使用case
只取前五个记录:
select e.id_Staff,
count(*) as cnt,
listagg(case when seqnum <= 5 then e.employer_name || '('|| e.job_title || ')' end, ', ')
within group (order by e.employer_name) AS Employ_info
from (select e.*,
row_number() over (partition by e.id_staff order by e.id_staff) as seqnum
from (select distinct em.id_Staff, em.employer_name, em.job_title, em_job_status
from employment em
where em.job_status = 'Active'
) e
) e
group by e.id_Staff
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.