1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
with data as (
select 'A' as compte, 'virement' as type_operation, 4000 as montant, to_date('01-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'A' as compte, 'retrait' as type_operation, 1000 as montant, to_date('08-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'A' as compte, 'retrait' as type_operation, 1100 as montant, to_date('12-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'B' as compte, 'virement' as type_operation, 4000 as montant, to_date('01-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'B' as compte, 'retrait' as type_operation, 1000 as montant, to_date('08-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'B' as compte, 'retrait' as type_operation, 900 as montant, to_date('12-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'C' as compte, 'virement' as type_operation, 4000 as montant, to_date('01-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'C' as compte, 'retrait' as type_operation, 1000 as montant, to_date('08-01-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'C' as compte, 'retrait' as type_operation, 1100 as montant, to_date('12-02-2017', 'dd-mm-yyyy') as date_op from dual
union
select 'D' as compte, 'virement' as type_operation, 4000 as montant, to_date('01-01-2019', 'dd-mm-yyyy') as date_op from dual
union
select 'F' as compte, 'virement' as type_operation, 2000 as montant, to_date('01-01-2017', 'dd-mm-yyyy') as date_op from dual
)
select d.compte from data d,
lateral(select sum(d1.montant) as retraits from data d1 where d1.compte = d.compte and d1.type_operation = 'retrait'
and d1.date_op between d.date_op and d.date_op + 30
having(sum(d1.montant) > 2000)
) sd
where d.date_op between to_date('01-01-2017', 'dd-mm-yyyy') and to_date('01-01-2018', 'dd-mm-yyyy')
and d.type_operation = 'virement' and d.montant > 3000 ; |