SQLD자격증공부/SQL기본
NVL, NVL2, Decode, Case when, rownum, rowid, with
김옹희
2023. 5. 8. 15:59
1. NVL
- NVL은 null인 값을 제거 하기 위한 일반 함수 이다.
- Null 대신 특정 숫자나 다른 값을 출력 할 수 있다.
- NVL(값, 'null일 때 출력 값')
2. NVL2
- NVL2는 NVL에 비해 파라미터가 하나 더 있다.
- NVL(값, 'null이 아닐 때 출력 값', 'null일 때 출력 값')
select id, name, nvl(bonus_type, '해당없음') as NVL_, nvl2(bonus, '보너스 대상자', '미대상자') as NVL2_ from salary; |
3. Decode
- Decode는 한 컬럼에 여러가지 조건을 적용하여 출력 할 때 사용한다.
- Decode(값, 조건1, 결과1, 조건2, 결과2...)
select id, name, decode(bonus_type, 'AB', '1등급', 'AC', '2등급', 'FA', '4등급', '미대상자') as decode_ from salary; |
4. Case when
- 여러가지 컬럼 및 조건으로 다양한 조건을 적용해야 할 때 사용한다.
- 하나의 컬럼 값으로 비교 할 때와 여러 컬럼으로 비교 할 때와 문법을 두가지로 나눠 사용 할 수 있다.
- 나열된 순으로 조건 비교를 한다.
select id, name, case bonus_type when 'AB' then '1등급' when 'AC' then '2등급' when 'FA' then '4등급' else '미 대상자' end as case_, case when bonus_type = 'AB' then '1등급' when bonus_type = 'AC' then '2등급' when bonus_type = 'FA' then '4등급' else '미 대상자' end as case2_ from salary; |
5. rownum
- Oracle에서 출력 rows를 설정 하는 기능 이다.
- Mssql 에서는 top() 기능으로 대체하여 사용 할 수 있다.
select * from salary where rownum < 3; select top(2) * from salary; |
6. rowid
- Oracle에서 row가 생성 할 때 부여되는 row의 고유 id.
- Rowid로 조건을 입력 할 수도 있다.
select rowid, se.* from salary se; |
7. with
- 일종의 임시적인 view 테이블이다.
- 특정 sql 블록의 재사용을 위해 사용된다.
- SQL 실행 속도에 유리하게 작용 할 수 있다.
- 여러 번 사용될 수록 유리하다.
with sql1 as (select * from salary where rownum < 3), sql2 as (select * from salary where rownum < 2) select * from sql1 union all select * from sql2; |