我有一个联接,它与一个表共享一对多关系。我不想返回这个值:
125|PROGRAM1|OEM2|1
125|PROGRAM1|OEM2|2
125|PROGRAM1|OEM2|3
我想返回这样一行:
125|PROGRAM1|OEM2|1,2,3
这是我现在运行的sql:
select d.layout_id,d.pgm_nm,d.corp_nm from io_layout_output d
join (select f.program_nm, c.corp_nm from file_config f
join corp_config c
on f.output_id = c.output_id
where f.output_id = 112) b
on d.pgm_nm = b.program_nm and d.corp_nm = b.corp_nm
我将如何获得正确的输出?
您需要使用一个函数。参见LISTAGG
select d.layout_id,
d.pgm_nm,
LISTAGG(d.corp_nm, ', ') as corp_nm
from io_layout_output d
join (select f.program_nm,
c.corp_nm
from file_config f
join corp_config c
on f.output_id = c.output_id
where f.output_id = 112) b
on d.pgm_nm = b.program_nm
and d.corp_nm = b.corp_nm
group by d.layout_id,
d.pgm_nm
编辑:
上次我使用Oracle时,您可以使用group by使用LISTAGG。我只是看了一下文档,它不再提及它。如果上述方法不起作用,请使用以下方法:
select d.layout_id,
d.pgm_nm,
LISTAGG(d.corp_nm, ', ')
WITHIN GROUP (ORDER BY d.layout_id, d.pgm_nm) as corp_nm
from io_layout_output d
join (select f.program_nm,
c.corp_nm
from file_config f
join corp_config c
on f.output_id = c.output_id
where f.output_id = 112) b
on d.pgm_nm = b.program_nm
and d.corp_nm = b.corp_nm
注意:我只是展示如何使用这个函数。根本没有查看您的查询。添加此注释是因为您的结果数据与SQL 上的列数不匹配