반응형

SQL에서 기본적으로 세로로 출력되는 리스트를 가로 형태로 출력을 해야 할 경우

 

 


-- 1 --

SELECT AGE,SEX, COUNT(*) AS CNT
FROM   TABLE_SAMPLE
WHERE  TR_DT BETWEEN '20000101' AND '20201231'
GROUP  BY AGE, SEX;


-- 2 --

SELECT AGE, DECODE(SEX, 'M', CNT) AS M_CNT, DECODE(SEX, 'F', CNT) AS F_CNT
FROM (
    SELECT AGE, SEX, COUNT(*) AS CNT
    FROM TABLE_SAMPLE
    WHERE TR_DT BETWEEN '20000101' AND '20201231'
    GROUP BY AGE, SEX
);


-- 3 --

SELECT AGE, MIN(DECODE(SEX, 'M', CNT)) AS M_CNT, MIN(DECODE(SEX, 'F', CNT)) AS F_CNT
FROM (
    SELECT AGE, SEX, COUNT(*) AS CNT
    FROM TABLE_SAMPLE
    WHERE TR_DT BETWEEN '20000101' AND '20201231'
    GROUP BY AGE, SEX
)GROUP BY AGE
ORDER BY AGE;


 

 

 

 

 

반응형
Posted by 공간사랑
,