우리집아찌 님이랑 똑같이 만들어져서 다르게 만들어봤습니다. 깔끔하지가 못하네요...
WITH T AS ( SELECT '201707' YYYYMM, 'A' DATA_STRING FROM DUAL UNION ALL SELECT '201707' YYYYMM, 'B' DATA_STRING FROM DUAL UNION ALL SELECT '201708' YYYYMM, 'A' DATA_STRING FROM DUAL UNION ALL SELECT '201709' YYYYMM, 'B' DATA_STRING FROM DUAL ) SELECT TT.YYYYMM, CASE WHEN DATA_STRING_CNT > 1 THEN REPLACE(REPLACE(DATA_STRING, ',' || 'A', ''), ',', '') ELSE REPLACE(DATA_STRING, ',', '') END DATA_STRING FROM (SELECT T.YYYYMM, ','|| WM_CONCAT(T.DATA_STRING) AS DATA_STRING, COUNT(T.DATA_STRING) DATA_STRING_CNT FROM T GROUP BY T.YYYYMM ) TT
/* 하나더 */ WITH T AS ( SELECT '201707' YYYY , 'A' GB FROM DUAL UNION ALL SELECT '201707' , 'B' FROM DUAL UNION ALL SELECT '201708' , 'A' FROM DUAL UNION ALL SELECT '201709' , 'B' FROM DUAL U ) SELECT A.YYYY , A.GB FROM ( SELECT YYYY , GB , ROW_NUMBER() OVER(PARTITION BY YYYY ORDER BY DECODE(GB,'B',1,'C',2)) RN FROM T ) A WHERE RN = 1 ORDER BY A.YYYY