【发布时间】:2018-09-03 09:50:27
【问题描述】:
我在 APEX 5.0 中有这样的报告查询:
WITH inner_table AS
( select distinct
i.ID
,i.name
,i.lastname
,case i.gender
when 'm' then 'Male'
when 'f' then 'Female'
end gender
,i.username
,b.name region
,i.address
,i.city city
,i.EMAIL
,r.name as "ROLE"
,ie.address as "region_location"
,case
when i.gender='m' THEN 'blue'
when i.gender='f' THEN '#F6358A'
END i_color
,b.course as COURSE
,si.city UNIVERSITY
,case
when i.id in (select app_user from scholarship) then 'check'
else 'close'
end as scholarship,
case
when i.id in (select ieur.app_user from ie_user_role ieur where role=4) then 'Admin'
else ''
end admin,
apex_item.checkbox(10, i.id, 'UNCHECKED onclick="highlightRow(this);"') as Del_usr
from app_users i left join regions b on (i.region=b.id)
left join ie_user_role ur on (i.id = ur.app_user)
left join ie_roles r on(ur.role = r.id)
left join user_house uh on (i.id=uh.app_user)
left join reg_location ie on (uh.house=ie.id)
left join study_list sl on i.id = sl.insan
left join study_institute si on sl.institute = si.id
left join course c on sl.course = c.id
where i.is_active='Y'
order by
i.name,i.lastname,i.username,region, city, i.EMAIL)
SELECT * FROM inner_table where (scholarship = :P5_SCHOLARSHIP or :P5_SCHOLARSHIP is null)
我可能会得到这样的结果:
|---------------------|------------------|-------|------------------|
| Name | Lastname | ... | Course |
|---------------------|------------------|-------|------------------|
| Some | User | ... | Course1 |
|---------------------|------------------|-------|------------------|
| Some | User | ... | Course2 |
|---------------------|------------------|-------|------------------|
但我想在同一行实现入伍课程,这是以前重复的,所以:
|---------------------|------------------|-------|------------------|
| Name | Lastname | ... | Course |
|---------------------|------------------|-------|------------------|
| Some | User | ... | Course1, Course2 |
|---------------------|------------------|-------|------------------|
我尝试使用 LISTAGG,但我没有记下我的尝试,所以很遗憾我现在无法发布。我基本上试过了:
,LISTAGG(b.course, ', ') within group (order by b.course) as COURSE
然后使用 COURSE 添加 GROUP BY,但在这种情况下,整个查询都会受到 GROUP BY 的影响,我必须正确应用其他列,对吗?否则会导致“ORA-00937: not a single-group group function”。我有点迷路了。
我尝试的另一件事是使用与上面相同的 LISTAGG 行的子查询表,并从子查询中获得想要的输出,但随后加入查询的其余部分并没有提供预期的结果。
我想在连接多个表时,我可以在这里为 LISTAGG 使用一些 SQL 帮助。
谢谢。
【问题讨论】:
标签: sql oracle oracle-apex-5