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 CMD (IDCLI, NumCMD, Art, Prix, Livreur) AS
(
select 1, 100, 'PZ', 12, 'Toto' union all
select 1, 100, 'ST', 21, 'Toto' union all
select 2, 101, 'RT', 5.5, 'Tata' union all
select 1, 102, 'RT', 5.5, 'Tata' union all
select 3, 103, 'OP', 10, 'Toto' union all
select 1, 104, 'PZ', 12, 'Toto' union all
select 1, 104, 'LM', 25.5, 'Toto'
),
REG (IDCLi, NumCMD, MontantGlobal, MtEsp, MtChq, MtCB) AS
(
select 1, 100, 33, 33, 0, 0 union all
select 2, 101, 5.5, 5.5, 0, 0 union all
select 1, 102, 5.5, 5.5, 0, 0 union all
select 3, 103, 10, 0, 10, 0 union all
select 1, 104, 37.5, 7.5, 0, 20
),
LIV AS (select distinct Livreur from CMD),
CMD_ENTETE AS (select distinct IDCLI, NumCMD, Livreur FROM CMD)
select
L.Livreur,
sum(R.MontantGlobal) MntGlobal
from
LIV L
inner join CMD_ENTETE E on E.Livreur = L.Livreur
inner join REG R on R.NumCMD = E.NumCMD
group by
L.Livreur |
Partager