LEVELNO | SEQ | UNIT | PART | EQUIP |
1 | 1 | 10 | ||
1 | 2 | 20 | ||
2 | 1 | 10 | A | |
2 | 2 | 10 | B | |
3 | 1 | 10 | A | 001-1 |
3 | 2 | 10 | A | 002-1 |
이런 데이터가 있는데.. 정렬을 하려고 합니다.
LEVELNO 는 트리의 DEPTH 개념이고 SEQ 는 순차적으로 증가합니다.
UNIT , PART , EQUIP 는 데이터 인데
원하는 결과 값은
LEVELNO | SEQ | UNIT | PART | EQUIP |
1 | 1 | 10 | ||
2 | 1 | 10 | A | |
3 | 1 | 10 | A | 001-1 |
3 | 2 | 10 | A | 002-1 |
2 | 2 | 10 | B | |
1 | 2 | 20 |
즉, LEVELNO 가 1이면 UNIT 에만 데이터가 들어오고
2이면 UNIT 과 PART 에 데이터가 들어옵니다.
ㄱ간단한것 같은데..뭔가 헤깔리네요 ;;
-------------------
추가.. 빨간색 부분에 오타가 있었네요..
WITH T AS ( SELECT '1' LEVELNO , '1' SEQ , '10' UNIT , '' PART , '' EQUIP FROM DUAL UNION ALL SELECT '1' LEVELNO , '2' SEQ , '20' UNIT , '' PART , '' EQUIP FROM DUAL UNION ALL SELECT '2' LEVELNO , '1' SEQ , '10' UNIT , 'A' PART , '' EQUIP FROM DUAL UNION ALL SELECT '2' LEVELNO , '2' SEQ , '20' UNIT , 'B' PART , '' EQUIP FROM DUAL UNION ALL SELECT '3' LEVELNO , '1' SEQ , '10' UNIT , 'A' PART , '001-1' EQUIP FROM DUAL UNION ALL SELECT '3' LEVELNO , '2' SEQ , '10' UNIT , 'A' PART , '002-1' EQUIP FROM DUAL ) SELECT * FROM T ORDER BY SEQ , UNIT
WITH T AS
(
SELECT '1' LEVELNO , '1' SEQ , '10' UNIT , '' PART , '' EQUIP FROM DUAL UNION ALL
SELECT '1' LEVELNO , '2' SEQ , '20' UNIT , '' PART , '' EQUIP FROM DUAL UNION ALL
SELECT '2' LEVELNO , '1' SEQ , '10' UNIT , 'A' PART , '' EQUIP FROM DUAL UNION ALL
SELECT '2' LEVELNO , '2' SEQ , '10' UNIT , 'B' PART , '' EQUIP FROM DUAL UNION ALL
SELECT '3' LEVELNO , '1' SEQ , '10' UNIT , 'A' PART , '001-1' EQUIP FROM DUAL UNION ALL
SELECT '3' LEVELNO , '2' SEQ , '10' UNIT , 'A' PART , '002-1' EQUIP FROM DUAL
)
SELECT *
FROM T
ORDER
BY SEQ
, UNIT
, PART NULLS FIRST
WITH T (LEVELNO, SEQ, UNIT, PART, EQUIP) AS ( SELECT 1, 1, 10, '' , '' FROM DUAL UNION ALL SELECT 1, 2, 20, '' , '' FROM DUAL UNION ALL SELECT 2, 1, 10, 'A', '' FROM DUAL UNION ALL SELECT 2, 2, 10, 'B', '' FROM DUAL UNION ALL SELECT 3, 1, 10, 'A', '001-1' FROM DUAL UNION ALL SELECT 3, 2, 10, 'A', '002-1' FROM DUAL ) SELECT * FROM T ORDER BY (CASE WHEN MOD(SEQ,2)=1 THEN LEVELNO END) ASC, (CASE WHEN MOD(SEQ,2)=0 THEN LEVELNO END) DESC
WITH t AS ( SELECT 1 levelno, 1 seq, '10' unit, '' part, '' equip FROM dual UNION ALL SELECT 1, 2, '20', '' , '' FROM dual UNION ALL SELECT 2, 1, '10', 'A', '' FROM dual UNION ALL SELECT 2, 2, '10', 'B', '' FROM dual UNION ALL SELECT 3, 1, '10', 'A', '001-1' FROM dual UNION ALL SELECT 3, 2, '10', 'A', '002-1' FROM dual ) SELECT t0.* FROM t t0 , t t1 , t t2 WHERE t1.unit(+) = t0.unit AND t1.levelno(+) = 1 AND t2.part(+) = t0.part AND t2.levelno(+) = 2 ORDER BY NVL(t1.seq, 0) , NVL(t2.seq, 0) , DECODE(t0.levelno, 3, t0.seq, 0) ;
-- 마농님 WITH 사용 했습니다. WITH t AS ( SELECT 1 levelno, 1 seq, '10' unit, '' part, '' equip FROM dual UNION ALL SELECT 1, 2, '20', '' , '' FROM dual UNION ALL SELECT 2, 1, '10', 'A', '' FROM dual UNION ALL SELECT 2, 2, '10', 'B', '' FROM dual UNION ALL SELECT 3, 1, '10', 'A', '001-1' FROM dual UNION ALL SELECT 3, 2, '10', 'A', '002-1' FROM dual ) SELECT * FROM T ORDER BY SEQ, UNIT, NVL(PART,'0'), NVL(EQUIP,'000-0') ;
한가지 더 질문 드리면.. 해당 데이터 윗쪽에
union 이 있습니다.
SELECT * FROM ( SELECT ... FROM DUAL UNION SELECT ... FROM T WHERE .. ORDER BY 답변 받은 부분 ) 이렇게 해보니 ORDER BY 항목은 SELECT 목록 식의 수라야 합니다. 라는 에러가 납니다 SELECT * FROM ( SELECT ... FROM DUAL UNION SELECT ... FROM T WHERE .. ) ORDER BY 답변 받은 부분
이렇게 괄호 부분을 변경해 보니 정렬 부분이 틀어지는데.. 좋은 방법 없을까요?