WITH T AS ( SELECT 'A' NM, '가' STR FROM DUAL UNION ALL SELECT 'A' NM, '나' STR FROM DUAL UNION ALL SELECT 'A' NM, '다' STR FROM DUAL UNION ALL SELECT 'B' NM, '가' STR FROM DUAL UNION ALL SELECT 'B' NM, '나' STR FROM DUAL UNION ALL SELECT 'B' NM, '다' STR FROM DUAL UNION ALL SELECT 'B' NM, '라' STR FROM DUAL UNION ALL SELECT 'C' NM, '가' STR FROM DUAL UNION ALL SELECT 'C' NM, '나' STR FROM DUAL UNION ALL SELECT 'C' NM, '다' STR FROM DUAL UNION ALL SELECT 'C' NM, '라' STR FROM DUAL UNION ALL SELECT 'D' NM, '가' STR FROM DUAL UNION ALL SELECT 'D' NM, '나' STR FROM DUAL ) SELECT DECODE(NM,'A',STR) AS A ,DECODE(NM,'B',STR) AS B ,DECODE(NM,'C',STR) AS C ,DECODE(NM,'D',STR) AS D FROM T;
WITH T AS ( SELECT 'A' NM, '1' NUM FROM DUAL UNION ALL SELECT 'B' NM, '2' NUM FROM DUAL UNION ALL SELECT 'C' NM, '3' NUM FROM DUAL UNION ALL SELECT 'D' NM, '4' NUM FROM DUAL UNION ALL SELECT 'E' NM, '5' NUM FROM DUAL ) SELECT MAX(DECODE(NM,'A',NUM)) A ,MAX(DECODE(NM,'B',NUM)) B ,MAX(DECODE(NM,'C',NUM)) C ,MAX(DECODE(NM,'D',NUM)) D ,MAX(DECODE(NM,'E',NUM)) E FROM T
이 쿼리에서는 GROUP BY를 안쓰고 해도 MAX로 한줄만 표시되서 이해하기 쉬웠는데
'가', '나', '다' 같이 여러개의 데이터가 모두 보이게 하는부분은...잘 이해가 안되네요 ㅠ
자세히 설명좀 부탁드립니다~
WITH T AS ( SELECT 'A' NM, '1' NUM FROM DUAL UNION ALL SELECT 'B' NM, '2' NUM FROM DUAL UNION ALL SELECT 'C' NM, '3' NUM FROM DUAL UNION ALL SELECT 'D' NM, '4' NUM FROM DUAL UNION ALL SELECT 'E' NM, '5' NUM FROM DUAL ) SELECT MAX(DECODE(NM,'A',NUM)) A ,MAX(DECODE(NM,'B',NUM)) B ,MAX(DECODE(NM,'C',NUM)) C ,MAX(DECODE(NM,'D',NUM)) D ,MAX(DECODE(NM,'E',NUM)) E FROM T