编写 Oracle Left Join 以生成非规范化分隔列

Writing a Oracle Left Join to produce a denormalized delimited column

鉴于:

Table_X

id        
-------
 1       
 2 
 3       
 4        

Table_Y

id_foreign   | content
-------------+-------------------
1            | A
1            | B
1            | C
1            | D
4            | E  
6            | F  
6            | G  

常规 left join Table_Y ON Table_X.id = Table_Y.id_foreign 会产生标准化输出,但这不是我最终想要的。是否可以在 SQL 之外不进行 post 处理的情况下轻松生成以下内容?内容字段被非规范化并由定界符分隔:

id           | content
-------------+-------------------
1            | A,B,C,D
2            | 
3            | 
4            | E

您正在寻找listagg()

select x.id, listagg(y.content, ',') within group (order by y.content)
from table_x x left join
     table_y y 
     on x.id = y.id_foreign
group by x.id;