CASE..WHEN..THEN..ELSE..# case when dt < '2024-01-01' then 1.18 else 1.10 end Cela ressemble à l'expression ternaire dans les langages de programmation classiques. Catégoriser des dates case when dt < '2023-08-01' then '1. Avant' when dt < '2023-09-01' then '2. Pendant' when dt >= '2023-09-01' then '3. Apres' end periode Segementer la base client suivant les montants d'achats case when total_amount > 0 and total_amount < 5000 then 'LVC' when total_amount >= 5000 and total_amount < 70000 then 'MVC' when total_amount >= 70000 then 'HVC' else 'Inactive' end as segment ```sql title = "Pivot Table" select dt, sum(case when product_category = 'Data' then amount else 0 end) data_pack_amount, sum(case when product_category = 'Voice' then amount else 0 end) voice_pack_amount, sum(case when product_category = 'Combo' then amount else 0 end) combo_pack_amount, sum(case when product_category = 'Airtime' then amount else 0 end) airtime_amount, sum(case when product_category = 'Other' then amount else 0 end) other_amount from edw.fct_orders where dt = rundt group by 1 ## Structure conditionnelle `IF..THEN..` La structure conditionnelle `IF` permet d'exécuter des instructions lorsqu'une condition est vérifiée. ```sql title="Structure conditionnelle" linenums="1" do $$ declare age integer; begin age := 35; if age >= 18 then raise info 'You are major !'; end if; end; $$; Structure alternative# Structure conditionnelle avec alternative 1 2 3 4 5 6 7 8 9 10 11 12do $$ declare age integer = 35; begin if age >= 18 then raise info 'You are major !'; else raise info 'You are minor !'; end if; end; $$; Structure conditionnelle imbriquée# Dans le cadre de problèmes complexes, il peut être nécessaire Structure conditionnelle avec alternative 1 2 3 4 5 6 7 8 9 10 11 12 13 14do $$ declare age integer = 35; begin if age >= 25 then raise info 'You are adult !'; elif age >= 18 then raise info 'You are major !'; else raise info 'You are minor !'; end if; end; $$; Conditions imbriquées# Dans le cadre de problèmes complexes, il peut être nécessaire Structure conditionnelle avec alternative 1 2 3 4 5 6 7 8 9 10 11 12do $$ declare age integer = 35; begin if age >= 18 then raise info 'You are major !'; else raise info 'You are minor !'; end if; end; $$; Applications# if rundt >= '2024-01-01' then drop table if exists tmp.store_orders_stg; create table tmp.store_orders_stg as ( select * from prod.store_orders t where order_date >= rundt and order_date < rundt + 1 ); else drop table if exists tmp.store_orders_stg; create table tmp.store_orders_stg as ( select * from stagging.orders t where order_date >= rundt and order_date < rundt + 1 ); end if; Arrêter l'execution pour certaines plages d'heures par exemple if not (to_char(now(), 'HH24') between '06' and '20') then return 'N'; end if; Arrêter l'execution lorsque les données source ne sont pas disponibles --- Compter le nombre de lignes select count(*) into _cnt from tmp.store_orders_stg where dt = rundt; if _cnt = 0 then raise notice 'Data not found !'; return 'N'; end if; .