Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- NPM
- Eclipse
- Java
- mapreduce
- Express
- window
- hadoop
- 공정능력
- SSL
- Kotlin
- SQL
- mybatis
- vaadin
- plugin
- es6
- tomcat
- GIT
- JavaScript
- R
- IntelliJ
- table
- Android
- 보조정렬
- Sqoop
- xPlatform
- MSSQL
- react
- Spring
- Python
- SPC
Archives
- Today
- Total
DBILITY
sql pivot row to column (행을 열로) - 구구단예제 본문
반응형
응용에 필요한 기초라 작성해 둔다. 나이가 들수록 순간적으로 기억이 나지 않는다.ㅠㅠ
- 첫번째
WITH T1 AS ( SELECT LEVEL NO FROM DUAL CONNECT BY LEVEL <=9 ) SELECT SUBSTR(RN,2,1) AS NO, MIN(CASE WHEN RN <= 19 THEN MT END) AS "1단", MIN(CASE WHEN RN BETWEEN 21 AND 29 THEN MT END) AS "2단", MIN(CASE WHEN RN BETWEEN 31 AND 39 THEN MT END) AS "3단", MIN(CASE WHEN RN BETWEEN 41 AND 49 THEN MT END) AS "4단", MIN(CASE WHEN RN BETWEEN 51 AND 59 THEN MT END) AS "5단", MIN(CASE WHEN RN BETWEEN 61 AND 69 THEN MT END) AS "6단", MIN(CASE WHEN RN BETWEEN 71 AND 79 THEN MT END) AS "7단", MIN(CASE WHEN RN BETWEEN 81 AND 89 THEN MT END) AS "8단", MIN(CASE WHEN RN BETWEEN 91 AND 99 THEN MT END) AS "9단" FROM ( SELECT T1.NO||T2.NO RN,T1.NO||'*'||T2.NO||'='||LPAD(T1.NO*T2.NO,2,' ') AS MT FROM T1,T1 T2 ) GROUP BY SUBSTR(RN,2,1) ORDER BY SUBSTR(RN,2,1)
- 두번째 : 11G부터는 PIVOT함수가 지원됩니다.
WITH T1 AS ( SELECT LEVEL NO FROM DUAL CONNECT BY LEVEL <=9 ) SELECT * FROM ( SELECT SUBSTR(RN,2,1) AS NO, ( CASE WHEN RN <= 19 THEN '1단' WHEN RN BETWEEN 21 AND 29 THEN '2단' WHEN RN BETWEEN 31 AND 39 THEN '3단' WHEN RN BETWEEN 41 AND 49 THEN '4단' WHEN RN BETWEEN 51 AND 59 THEN '5단' WHEN RN BETWEEN 61 AND 69 THEN '6단' WHEN RN BETWEEN 71 AND 79 THEN '7단' WHEN RN BETWEEN 81 AND 89 THEN '8단' ELSE '9단' END ) AS DAN, MT FROM ( SELECT T1.NO||T2.NO RN,T1.NO||'*'||T2.NO||'='||LPAD(T1.NO*T2.NO,2,' ') AS MT FROM T1,T1 T2 ) ) PIVOT ( MIN(MT) FOR DAN IN ('1단','2단','3단','4단','5단','6단','7단','8단','9단') ) ORDER BY NO
반응형
'database > sql' 카테고리의 다른 글
oracle 점이력 선분이력으로 만들기 (0) | 2016.10.10 |
---|---|
현재시간 1초 전 구하기 (0) | 2016.10.10 |
세로를 5줄로만 표현하기 (ROW 고정) (0) | 2016.10.07 |
현재시간기준 (3)교대일자,교대구분 구하기 (0) | 2016.10.05 |
oracle unpivot string comma separate to row (0) | 2016.09.07 |
Comments