수업/SQL
DECODE(A, B, X, Y), 정렬(오름차순 내림차순)/ 강의 시간 예제
케잉
2023. 11. 21. 23:44
강의시간과 학점이 같으면 '일반과목'을 리턴 받은 후 정렬도 하고 싶다면?
강의시간: lec_time
학점: lec_point
DECODE(A, B, X, Y) |
A = B 이면 X를 출력,
A ≠ B 이면 Y를 출력
|
같으면 -> decode (lec_time,lec_point,'일반과목',0)
--같을때만 값을 주었으니 다를때는 무조건 null이다
1단계 - 일반과목 출력
SELECT
decode(lec_time,lec_point,'일반과목',null)
FROM lecture
2단계 - null의 내림차순
SELECT
decode(lec_time,lec_point,'일반과목',null)
FROM lecture
ORDER BY decode(lec_time,lec_point,'일반과목',null) desc;
- null = 모른다 , 결정되지 않았다 . 정렬을 할 수 없다
- 그래서 맨 뒤에 붙였다
- 그래서 null인 컬럼을 내림차순으로 정렬하면 null이 맨 앞에 왔다
3단계 - null의 오름차순
SELECT
decode(lec_time,lec_point,'일반과목',null)
FROM lecture
ORDER BY decode(lec_time,lec_point,'일반과목',null) asc;