7 Aralık 2018 Cuma

CASE...WHEN

Giriş
Sözdizimi şöyle
CASE WHEN Sütun=Değer THEN YeniDeğer END
Örnek
Açıklaması şöyle
You see in the CASE statement below, we’re categorizing users based on if they are paying customers or not. We then apply a sum() since it’s a quick way to count the number of paying customers vs non-paying customers in one simple query. If we did not have the CASE statement, it would take us two queries to find both numbers.
Şöyle yaparız
SELECT 
  date, 
  SUM(
    CASE WHEN paying_customer = 'yes' THEN downloads END
  ) AS paying, 
  SUM(
    CASE WHEN paying_customer = 'no' THEN downloads END
  ) AS non_paying 
FROM 
  ms_user_dimension a
Örnek
Şöyle yaparız.
SELECT top, nd, 
       CASE WHEN top = 1 THEN 'T' 
            WHEN nd = 1 THEN 'N'
       END AS topnd
FROM table1
Çıktı olarak şunu alırız.
top     nd  topnd
1       0   T
0       1   N
0       1   N
1       1   T
Örnek
Şöyle yaparız.
select (case when col0 = 'a' then 1 end) as alarm_ID, 
       (case when col0 = 'c' then 1 end) as CCTV_ID, 
       q.last_maintenance_date as Date 
from ...;

Hiç yorum yok:

Yorum Gönder