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
| with cte_TExtrait (Compte, NoExtrait, Date, RefExtrait, rn) as -- Affectation d'un classement trié par date descendante puis par NoExtrait
(
select Compte, NoExtrait, Date, RefExtrait
, row_number() over(partition by Compte order by Date desc, NoExtrait desc)
from Textrait
)
, cte_TExtrait_last (Compte, NoExtrait, Date, RefExtrait) as -- Récupération des données Extrait uniquement pour le premier classement
(
select Compte, NoExtrait, Date, RefExtrait
from cte_TExtrait
where rn = 1
)
, cte_TLignExtrait_agg (NoExtrait, Montant) as -- Calcul du montant total pour les extraits concernés
(
select lge.NoExtrait
, sum(lge.Montant)
from TLignExtrait as lge
join cte_TExtrait_last as ctl on ctl.NoExtrait = lge.NoExtrait
group by lge.NoExtrait
)
select cpt.Banque -- Derniere étape de présentation des données
, cpt.Compte
, ctl.NoExtrait as NoDernExtr
, ctl.Date
, ctl.RefExtrait
, clg.Montant as MontTotal
from Tcpte as cpt
join cte_TExtrait_last as ctl on ctl.Banque = cpt.Banque
join cte_TLignExtrait_agg as clg on clg.NoExtrait = ctl.NoExtrait; |
Partager