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 33
| ;with
CLD as
( select distinct
year(DATE_MVT) as ANNEE
, month(DATE_MVT) as MOIS
from T_Mvt
where year(DATE_MVT) in (2017, 2018, 2019)
)
, MVT as
( select CLE_PIECE
, year(DATE_MVT) as ANNEE
, month(DATE_MVT) as MOIS
,QTE
from T_MVT mvt
where mvt.qte < 0
AND MVT.TYPE_MVT in (1, 3)
)
select PCE.CODE
, PCE.DESIGN
, ISNULL(sum(MVT.QTE), 0) as [qté/mois]
, CLD.MOIS as [mois]
, CLD.ANNEE as [année]
from T_PIECE as PCE
cross join CLD
left join MVT
on MVT.CLE_PIECE = PCE.CLE_PIECE
and MVT.ANNEE = CLD.ANNEE
and MVT.MOIS = CLD.MOIS
group by PCE.CODE
, PCE.DESIGN
, CLD.MOIS
, CLD.ANNEE
order by PCE.CODE, CLD.ANNEE, CLD.MOIS |
Partager