Oracle:Group By 后将某一列整合成一个字段;同时置于In中
一、使用 listagg() WITHIN GROUP () 将多行合并成一行
SELECT SUM(H.NUM) TOTAL_NUM,
LISTAGG(H.DETAIL_ID, ',') WITHIN GROUP(ORDER BY H.A, H.B, H.C) DETAIL_IDS, H.A, H.B, H.C FROM TABLE1 H WHERE H.ID= '1' GROUP BY H.A, H.B, H.C
二、将字符串使用逗号分隔并放于IN查询语句中
SELECT CEIL(NVL(SUM(ABS(H.NUM)), 0) / OPPACK) --CEIL 向上取整 NVL 校验是否未NULL FROM TABLE2 H WHERE H.RELATION_DETAIL_ID IN (SELECT REGEXP_SUBSTR(G.DETAIL_IDS, '[^,]+', 1, LEVEL) FROM DUAL CONNECT BY REGEXP_SUBSTR(G.DETAIL_IDS, '[^,]+', 1, LEVEL) IS NOT NULL)