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 34 35 36
|
SELECT
c1
, c2
, ...
, cn
, Quantite * SUM(ISNULL(Month1, 0)) AS Month1
, Quantite * SUM(ISNULL(Month2, 0)) AS Month2
, ...
FROM [Tables jointes]
CROSS APPLY (
SELECT
*
FROM (
SELECT 'Month1', 1
WHERE laDate >= @mois1
AND laDate < DATEADD(mm, 1, @mois1)
UNION ALL
SELECT 'Month2', 1
WHERE laDate >= @mois2
AND laDate < DATEADD(mm, 1, @mois2)
UNION ALL
SELECT 'Month3', 1
WHERE laDate >= @mois2
AND laDate < DATEADD(mm, 1, @mois3)
[...]
) AS X (m, v)
PIVOT (MIN(v) FOR m IN ([Month1], [Month2], ...)) AS Y
) AS X
GROUP BY c1, c2, ..., cn |
Partager