1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| WITH comptages as (
SELECT t1.FAMILLE as FA,t2.DAT as DAT,t2.MATRI AS MATRI,t3.NOM AS NOM,t4.MOTIF AS MOTIF,
sum(case when t1.FAMILLE = 'PR' then 1 else 0 end) over (partition by t2.matri) cnt_PR,
sum(case when t4.MOTIF = 'A1' then 1 else 0 end) over (partition by t2.matri) cnt_A1,
sum(case when t4.MOTIF = 'A1' then 1 else 0 end) over (partition by t2.matri) cnt_A2,
sum(case when t4.MOTIF = 'A1' then 1 else 0 end) over (partition by t2.matri) cnt_A3
FROM t1,t2, t3,t4
where t1.MOTIF = t4.MOTIF
AND t2.MATRI = t3.MATRI
AND t4.MATRI = t2.MATRI
AND t4.DAT = t2.DAT
AND (t1.FAMILLE in ('PR') OR t4.MOTIF in ('A1','A2','A3'))
AND t2.DAT = to_date('28/05/2020', 'dd/mm/yyyy')
)
SELECT FA,DAT,MATRI,NOM,MOTIF
FROM comptages
WHERE (motif = 'A1' and cnt_A1 <> 0)
OR (motif = 'A2' and cnt_A2 <> 0)
OR (motif = 'A3' and fa = 'PR' and cnt_A3 <> 0 and cnt_PR <> 0)
OR (motif not in ('A1', 'A2', 'A3')); |
Partager