데이터 양쪽으로 나누는 쿼리.. 도와주세요 0 5 3,316

by 망뎅이 [SQL Query] SQL QUERY 쿼리 [2012.12.13 08:44:57]


select 'col1' col, '홍길동' name from dual
union all
select 'col1' col, '강감찬' name from dual
union all
select 'col1' col, '김부식' name from dual
union all
select 'col1' col, '김동수' name from dual
union all
select 'col2' col, '둘리' name from dual
union all
select 'col2' col, '또치' name from dual
union all
select 'col3' col, '김춘삼' name from dual
union all
select 'col3' col, '김동식' name from dual
union all
select 'col3' col, '강동원' name from dual
union all
select 'col4' col, '이요원' name from dual
union all
select 'col4' col, '김태희' name from dual
union all
select 'col4' col, '김수지' name from dual

위와 같이 데이터가 있습니다.

제가 보여주고 싶은 데이터의 형식은 이렇습니다.

col name         col name
============================================================================
col1 홍길동 col2 둘리
col1 강감찬 col2 또치
col1 김부식
col1 김동수
col3 김춘삼 col4 이요원
col3 김동식 col4 김태희
col3 강동원 col4 김수지


위와 같이 데이터를 나타내고 싶은데 어렵네요... 도와주세요 ㅠㅠ

col name         col name
============================================================================
col1 홍길동 col2 둘리
col1 강감찬 col2 또치
col1 김부식 col4 이요원
col1 김동수 col4 김태희
col3 김춘삼 col4 김수지
col3 김동식
col3 강동원

또, 이렇게 데이터가 나와야 할 경우는 어떻게 해야할까요? 고수님들의 도움 부탁드리겠습니다. ㅠㅠ
 
by 손님 [2012.12.13 09:32:37]
 
WITH TB_TEST
AS
(
  select 'col1' col, '홍길동' name from dual 
  union all
  select 'col1' col, '강감찬' name from dual 
  union all
  select 'col1' col, '김부식' name from dual 
  union all
  select 'col1' col, '김동수' name from dual 
  union all
  select 'col2' col, '둘리' name from dual 
  union all
  select 'col2' col, '또치' name from dual 
  union all
  select 'col3' col, '김춘삼' name from dual 
  union all
  select 'col3' col, '김동식' name from dual 
  union all
  select 'col3' col, '강동원' name from dual 
  union all
  select 'col4' col, '이요원' name from dual 
  union all
  select 'col4' col, '김태희' name from dual 
  union all
  select 'col4' col, '김수지' name from dual
)
-- 첫번째 쿼리
SELECT MIN(DECODE(COL_NO, 1, COL , NULL)) AS COL1
   , MIN(DECODE(COL_NO, 1, NAME, NULL)) AS NAME1
   , MIN(DECODE(COL_NO, 2, COL , NULL)) AS COL2
   , MIN(DECODE(COL_NO, 2, NAME, NULL)) AS NAME2
FROM  (SELECT TB_TEST.*
        , RANK() OVER (PARTITION BY COL ORDER BY ROWNUM) AS RNUM
        , 2-MOD(SUBSTR(COL,4,1),2) AS COL_NO
        , DECODE(MOD(SUBSTR(COL,4,1),2),1, SUBSTR(COL,4,1), SUBSTR(COL,4,1) - 1) AS GRP_NO
     FROM  TB_TEST
     ORDER BY ROWNUM)
GROUP BY GRP_NO,RNUM

UNION ALL
SELECT '' AS COL1
   , '(구' AS NAME1
   , '분)' AS COL2
   , '' AS NAME2
FROM  DUAL
UNION ALL

-- 두번째 쿼리
SELECT MIN(DECODE(COL_NO, 1, COL , NULL)) AS COL1
   , MIN(DECODE(COL_NO, 1, NAME, NULL)) AS NAME1
   , MIN(DECODE(COL_NO, 2, COL , NULL)) AS COL2
   , MIN(DECODE(COL_NO, 2, NAME, NULL)) AS NAME2
FROM  (SELECT TB_TEST.*
        , RANK() OVER (PARTITION BY MOD(SUBSTR(COL,4,1),2) ORDER BY ROWNUM) AS RNUM
        , 2-MOD(SUBSTR(COL,4,1),2) AS COL_NO
        , DECODE(MOD(SUBSTR(COL,4,1),2),1, SUBSTR(COL,4,1), SUBSTR(COL,4,1) - 1) AS GRP_NO
     FROM  TB_TEST
     ORDER BY ROWNUM)
GROUP BY RNUM

;


by 망뎅이 [2012.12.13 10:16:10]
정말 정말 감사합니다~~ ㅋㅋ 
정렬이 안되어있어 깜짝 놀랐네요. ^^ ㅎㅎ 저런 쿼리를 짜려면 어떤식으로 사고를 해야 하나요? ㅠㅠ
제가 집합적인 사고가 좀 부족한 것 같아서 ㅠㅠ

by 아발란체 [2012.12.13 10:56:30]

뉴ㅅ뉴)ㆀ 회의 끝나고 오니 풀이가... 있군요. 그래도 풀어봐야지... + ㅁ +)/


by 아발란체 [2012.12.13 11:16:15]
--1번
WITH T AS (
  SELECT
    ROUND(DENSE_RANK() OVER(ORDER BY col) / 2) AS group_no,
    ROW_NUMBER() OVER(PARTITION BY col ORDER BY col) AS local_seq_no,
    MOD(DENSE_RANK() OVER(ORDER BY col), 2) AS pos_no,
    col,
    name
  FROM (
    SELECT 'col1' col, '홍길동' name FROM DUAL
    UNION ALL
    SELECT 'col1' col, '강감찬' name FROM DUAL
    UNION ALL
    SELECT 'col1' col, '김부식' name FROM DUAL
    UNION ALL
    SELECT 'col1' col, '김동수' name FROM DUAL
    UNION ALL
    SELECT 'col2' col, '둘리' name FROM DUAL
    UNION ALL
    SELECT 'col2' col, '또치' name FROM DUAL
    UNION ALL
    SELECT 'col3' col, '김춘삼' name FROM DUAL
    UNION ALL
    SELECT 'col3' col, '김동식' name FROM DUAL
    UNION ALL
    SELECT 'col3' col, '강동원' name FROM DUAL
    UNION ALL
    SELECT 'col4' col, '이요원' name FROM DUAL
    UNION ALL
    SELECT 'col4' col, '김태희' name FROM DUAL
    UNION ALL
    SELECT 'col4' col, '김수지' name FROM DUAL
  )
)
SELECT
  MAX(col1) AS col1,
  MAX(name1) AS name1,
  MAX(col2) AS col2,
  MAX(name2) AS name2
FROM (
  SELECT
    group_no, local_seq_no,
    DECODE(pos_no, 1, col) AS col1,
    DECODE(pos_no, 1, name) AS name1,
    DECODE(pos_no, 0, col) AS col2,
    DECODE(pos_no, 0, name) AS name2
  FROM
    T
) 
GROUP BY
  group_no, local_seq_no
ORDER BY
  group_no, local_seq_no

--2번
WITH T AS (
  SELECT
    ROWNUM AS rn,
    MOD(DENSE_RANK() OVER(ORDER BY col), 2) AS pos_no,
    col,
    name
  FROM (
    SELECT 'col1' col, '홍길동' name FROM DUAL
    UNION ALL
    SELECT 'col1' col, '강감찬' name FROM DUAL
    UNION ALL
    SELECT 'col1' col, '김부식' name FROM DUAL
    UNION ALL
    SELECT 'col1' col, '김동수' name FROM DUAL
    UNION ALL
    SELECT 'col2' col, '둘리' name FROM DUAL
    UNION ALL
    SELECT 'col2' col, '또치' name FROM DUAL
    UNION ALL
    SELECT 'col3' col, '김춘삼' name FROM DUAL
    UNION ALL
    SELECT 'col3' col, '김동식' name FROM DUAL
    UNION ALL
    SELECT 'col3' col, '강동원' name FROM DUAL
    UNION ALL
    SELECT 'col4' col, '이요원' name FROM DUAL
    UNION ALL
    SELECT 'col4' col, '김태희' name FROM DUAL
    UNION ALL
    SELECT 'col4' col, '김수지' name FROM DUAL
  )
)
SELECT
  pn,
  MAX(col1) AS col1,
  MAX(name1) AS name1,
  MAX(col2) AS col2,
  MAX(name2) AS name2
FROM (
  SELECT
    ROW_NUMBER() OVER(PARTITION BY pos_no ORDER BY rn) AS pn,
    DECODE(pos_no, 1, col) AS col1,
    DECODE(pos_no, 1, name) AS name1,
    DECODE(pos_no, 0, col) AS col2,
    DECODE(pos_no, 0, name) AS name2
  FROM
    T
)
GROUP BY
  pn

by 알콩 [2012.12.13 11:45:12]

여기서 많이 보고 따라하시다보면,

자주 쓰이는 방식이 있습니다.

쓰다보니 손에 조금씩 익네요.

그래도 아직도 찾아보면서 하고 있습니다.

화이팅!
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입