我正在尝试使用逗号分隔值列表创建联接。例如:
rule_id | attribute_id ---------------------- 1 | a 1 | b 2 | c 2 | d
应该:
rule_id | attribute_id ---------------------- 1 | a,b 2 | c,d
我正在尝试使用LISTAGG。但是,使用下面的代码,我得到了ORA-00937: not a single-group group function。我注意到了FOR PATHsql-server的语法,但是对于我们的配置而言,它看起来并不可行。这是我的查询:
LISTAGG
ORA-00937: not a single-group group function
FOR PATH
SELECT r.rule_id as RULE_ID, LISTAGG(a.ATTRIBUTE_ID, ', ') WITHIN GROUP (ORDER BY a.ATTRIBUTE_ID) "ATTR_IDS" FROM N_RULE r, N_ATTRIBUTE a WHERE r.RULE_ID = a.RULE_ID ORDER BY r.AUDIENCE, UPPER(r.NAME);
我认为要使您的查询正常工作,您需要添加一个group by,然后更改order by。您还应该使用适当的显式连接语法:
group by
order by
SELECT r.rule_id as RULE_ID, LISTAGG(a.ATTRIBUTE_ID, ', ') WITHIN GROUP (ORDER BY a.ATTRIBUTE_ID) as "ATTR_IDS" FROM N_RULE r JOIN N_ATTRIBUTE a ON r.RULE_ID = a.RULE_ID GROUP BY r.rule_id ORDER BY r.rule_id;
或者,可能要在结果中包括其他属性:
SELECT r.rule_id, r.AUDIENCE, UPPER(r.NAME) LISTAGG(a.ATTRIBUTE_ID, ', ') WITHIN GROUP (ORDER BY a.ATTRIBUTE_ID) as "ATTR_IDS" FROM N_RULE r JOIN N_ATTRIBUTE a ON r.RULE_ID = a.RULE_ID GROUP BY r.rule_id, r.AUDIENCE, UPPER(r.NAME) ORDER BY r.AUDIENCE, UPPER(r.NAME);