SELECT A.Company_ID, A.Reference, A.Year, A.months, ISNULL(C.Amount, 0) AS Amount
FROM
(SELECT DISTINCT Y.Company_ID, Y.Reference, G.[Year], D.months
FROM yearlySales Y,
(SELECT [Year]
FROM yearlySales
WHERE [Year] BETWEEN 2008 AND 2009
GROUP BY [Year] ) G,
(SELECT 1 AS months
UNION
SELECT 2 AS months
UNION
SELECT 3 AS months
UNION
SELECT 4 AS months
UNION
SELECT 5 AS months
UNION
SELECT 6 AS months
UNION
SELECT 7 AS months
UNION
SELECT 8 AS months
UNION
SELECT 9 AS months
UNION
SELECT 10 AS months
UNION
SELECT 11 AS months
UNION
SELECT 12 AS months) D
WHERE Y.Company_ID = 1
AND Y.Reference BETWEEN 14 AND 15 ) A
LEFT OUTER JOIN yearlySales C ON A.Company_ID = C.Company_ID
AND A.Reference = C.Reference
AND A.[Year] = C.[Year]
AND A.months = C.[Month]
ORDER BY A.Company_ID, A.Reference, A.Year, A.months
|