DB

[DB] Decode vs Case when

girin_dev 2024. 5. 21. 15:30
728x90
반응형

Decode

   decode(target_col,null,null,0,0,coalesce(target_col2 / target_col, 0))

 

Case when

case when target_col1 IS NULL then null
     when target_col1 = 0 then 0
     when (target_col1 is null and target_col1 != 0) then coalesce(target_col2 / target_col1,0)
    ELSE
     ....

 

 

1. 대규모 데이터 및 복잡한 쿼리인 경우 Case when의 속도가 더 빠름.

2. Case 문은 statement 이고 decode 는 func에 해당한다. 

3. Case 는 sql / pl/sql 모두 쓰이나, decode는 sql 에서만 쓰인다. 

4. Case는 when 이후 조건문이 가능하지만, decode는 동일값인지 여부만 판별 가능.

 

decode에 비해 Case 문이 가독성이 좋으므로, 선호한다 ( 속도는 미미하지만 덤으로 )

320x100
반응형