ROWNUM과 ORDER BY에대해 질문하고싶습니다. 0 6 5,244

by 여비몬 ORDER BY ROWNUM [2013.01.18 09:15:36]



SELECT
   MAX(DECODE(MOD(ROWNUM,5),1,B.CODE,' ')) AS CODE1    
, MAX(DECODE(MOD(ROWNUM,5),1,B.NAME,' ')) AS NAME1    
, MAX(DECODE(MOD(ROWNUM,5),2,B.CODE,' ')) AS CODE2
, MAX(DECODE(MOD(ROWNUM,5),2,B.NAME,' ')) AS NAME2
, MAX(DECODE(MOD(ROWNUM,5),3,B.CODE,' ')) AS CODE3
, MAX(DECODE(MOD(ROWNUM,5),3,B.NAME,' ')) AS NAME3
, MAX(DECODE(MOD(ROWNUM,5),4,B.CODE,' ')) AS CODE4
, MAX(DECODE(MOD(ROWNUM,5),4,B.NAME,' ')) AS NAME4
, MAX(DECODE(MOD(ROWNUM,5),0,B.CODE,' ')) AS CODE5
, MAX(DECODE(MOD(ROWNUM,5),0,B.NAME,' ')) AS NAME5
  FROM GOODDAY B 
 WHERE B.ESBD_CO_CODE > 0
 GROUP BY CEIL(ROWNUM/5)
 ORDER BY CODE1 ASC

굿모닝입니다!
제가 ROWNUM과 ORDER BY를 쓰려고합니다.
그런데 ORDER BY 이후에 ROWNUM이 적용된다고하는데
잘이해가 가지않아서요.. 가로로 5개씩뜨는 방식으로했습니다.

밑에사진은 제가 작성한쿼리문을했을때 작은번호부터 오름차순으로 정리를하려고하는데
중간에 ROWNUM이 먹히지않아서인지 다른게 끼어있네요ㅠㅠ

ROWNUM이랑 ORDER BY를 먹히게하려면 어떻게해야할까요..
by 제로 [2013.01.18 09:17:37]
인라인뷰로 감싸고 order by 하세요~

by 아발란체 [2013.01.18 09:32:48]
--ORDER BY와 ROWNUM를 같이 쓰면
--ROWNUM이 먼저 적용되고 ORDER BY 됩니다.

--즉 ORDER BY 이후에 ROWNUM이 적용된다는 말은
--지금 상황에서는
--ORDER BY CODE1 ASC 이후에 ROWNUM를 쓸 수 있다는 말로 해석하시면 될 것 같습니다.

--제로님이 말씀하신 것과 같은 얘기고요,

SELECT
	*
FROM (
	SELECT
		MAX(DECODE(MOD(ROWNUM,5),1,B.CODE,' ')) AS CODE1  
		, MAX(DECODE(MOD(ROWNUM,5),1,B.NAME,' ')) AS NAME1  
		, MAX(DECODE(MOD(ROWNUM,5),2,B.CODE,' ')) AS CODE2
		, MAX(DECODE(MOD(ROWNUM,5),2,B.NAME,' ')) AS NAME2
		, MAX(DECODE(MOD(ROWNUM,5),3,B.CODE,' ')) AS CODE3
		, MAX(DECODE(MOD(ROWNUM,5),3,B.NAME,' ')) AS NAME3
		, MAX(DECODE(MOD(ROWNUM,5),4,B.CODE,' ')) AS CODE4
		, MAX(DECODE(MOD(ROWNUM,5),4,B.NAME,' ')) AS NAME4
		, MAX(DECODE(MOD(ROWNUM,5),0,B.CODE,' ')) AS CODE5
		, MAX(DECODE(MOD(ROWNUM,5),0,B.NAME,' ')) AS NAME5
	FROM
		GOODDAY B 
	WHERE
		B.ESBD_CO_CODE > 0
	ORDER BY
		CODE1 ASC
)
GROUP BY
	CEIL(ROWNUM / 5)

by 여비몬 [2013.01.18 09:54:31]

음..정렬이안되네요ㅠㅠ
ORDER BY랑 ROWNUM이 같은레벨에있을경우 정렬이안된다고 어딘가에써있는걸봤는데
다른방식으로도해봐야겠어요ㅠㅠ
제로님 아발란체님 감사드립니다~

by 손님 [2013.01.18 09:58:41]
SELECT
  MAX(DECODE(MOD(ROWNUM,5),1,B.CODE,' ')) AS CODE1   
, MAX(DECODE(MOD(ROWNUM,5),1,B.NAME,' ')) AS NAME1   
, MAX(DECODE(MOD(ROWNUM,5),2,B.CODE,' ')) AS CODE2
, MAX(DECODE(MOD(ROWNUM,5),2,B.NAME,' ')) AS NAME2
, MAX(DECODE(MOD(ROWNUM,5),3,B.CODE,' ')) AS CODE3
, MAX(DECODE(MOD(ROWNUM,5),3,B.NAME,' ')) AS NAME3
, MAX(DECODE(MOD(ROWNUM,5),4,B.CODE,' ')) AS CODE4
, MAX(DECODE(MOD(ROWNUM,5),4,B.NAME,' ')) AS NAME4
, MAX(DECODE(MOD(ROWNUM,5),0,B.CODE,' ')) AS CODE5
, MAX(DECODE(MOD(ROWNUM,5),0,B.NAME,' ')) AS NAME5
  FROM (
  SELECT *
    FROM GOODDAY B 
   WHERE B.ESBD_CO_CODE > 0
   ORDER BY CODE1 ASC
  )
 GROUP BY CEIL(ROWNUM/5)
 ORDER BY CEIL(ROWNUM/5)

by 아발란체 [2013.01.18 11:14:43]
--아, 죄송합니다. 밖 부분만 보고 수정해서 올렸는데 안돌아가겠네요.
--다시 수정했습니다.
SELECT
  MAX(DECODE(MOD(ROWNUM,5),1,B.CODE,' ')) AS CODE1  
  , MAX(DECODE(MOD(ROWNUM,5),1,B.NAME,' ')) AS NAME1  
  , MAX(DECODE(MOD(ROWNUM,5),2,B.CODE,' ')) AS CODE2 
  , MAX(DECODE(MOD(ROWNUM,5),2,B.NAME,' ')) AS NAME2 
  , MAX(DECODE(MOD(ROWNUM,5),3,B.CODE,' ')) AS CODE3 
  , MAX(DECODE(MOD(ROWNUM,5),3,B.NAME,' ')) AS NAME3 
  , MAX(DECODE(MOD(ROWNUM,5),4,B.CODE,' ')) AS CODE4 
  , MAX(DECODE(MOD(ROWNUM,5),4,B.NAME,' ')) AS NAME4 
  , MAX(DECODE(MOD(ROWNUM,5),0,B.CODE,' ')) AS CODE5 
  , MAX(DECODE(MOD(ROWNUM,5),0,B.NAME,' ')) AS NAME5 
FROM ( 
  SELECT
    *
  FROM
    GOODDAY B 
  WHERE
    B.ESBD_CO_CODE > 0 
  ORDER BY
    CODE1 ASC
) 
GROUP BY
  CEIL(ROWNUM / 5)
ORDER BY
  CODE1 ASC 

by 여비몬 [2013.01.18 13:19:10]

아발란체님 세심하셔ㅠㅠ 감사합니다!
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입