간단한 쿼리 질문 0 4 1,054

by 권사마 [SQL Query] [2018.03.30 11:15:41]


 

 SELECT '20180001' AS T1,
            'TEST1' AS T2
     FROM DUAL
     UNION ALL
     SELECT '20180001' AS T1,
            'TEST2' AS T2
     FROM DUAL
     UNION ALL
     SELECT '20180002' AS T1,
            'TEST3' AS T2
     FROM DUAL
     UNION ALL
     SELECT '20180002' AS T1,
            'TEST4' AS T2
     FROM DUAL;

Q1. 위의 데이터에서 아래와 같이 T1컬럼 같은 숫자끼리 CNT 번호별로 보이게 쿼리할려면 어떻게 해야 할까요? T1컬럼의 정렬은 역순입니다ㅏ.. 간단한거 같으면서 어렵네요 ㅠㅠ

 

CNT T1 T2
1 20180002 TEST3
1 20180002 TEST4
2 20180001 TEST1
2 20180001 TEST2

 

by 우리집아찌 [2018.03.30 12:46:13]
WITH T AS (
 SELECT '20180001' AS T1,
            'TEST1' AS T2
     FROM DUAL
     UNION ALL
     SELECT '20180001' AS T1,
            'TEST2' AS T2
     FROM DUAL
     UNION ALL
     SELECT '20180002' AS T1,
            'TEST3' AS T2
     FROM DUAL
     UNION ALL
     SELECT '20180002' AS T1,
            'TEST4' AS T2
     FROM DUAL
)

SELECT B.RN CNT  
     , A.T1 
     , A.T2 
  FROM T A
     , (SELECT ROWNUM RN , T1 
          FROM (SELECT T1 FROM T GROUP BY T1 ORDER BY T1 DESC )
       ) B
  WHERE A.T1 = B.T1    
 ORDER BY A.T1 DESC
  
  

 


by 마농 [2018.03.30 13:00:36]
SELECT DENSE_RANK() OVER(ORDER BY t1 DESC) dr
     , t1, t2
  FROM t
;

 


by 우리집아찌 [2018.03.30 13:08:23]

아.. ㅋㅋ


by 권사마 [2018.03.30 19:11:17]

오~ 명쾌하군요~ ^^

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