Question : SQL Stored Procedure

I'm using SQL server 2008
I have a table with Columns "Company_ID", "Reference" , "Year", "Month", "Amount"
The table has only one row for "Company_ID", "Reference" , "Year", "Month"
I want to retrive the rows filtering by:
   Company_ID Min_Refernce Max_Reference Min_Year and Max_Year

And I want that if a reference has no row for a month it retrives that inexisting row with Amount=0

In the Code Sample there are some rows of two company_ID and I filtered it with:
Company_ID=1
Min_Reference=14
Max_Reference=15
Min_Year=2008
Max_Year=2009

In a standard Select it will retrive 8 rows
I want to retrive 2 references * 2 years * 12months = 48 rows
For example if I have:
Code Snippet:
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
Company_IDReferenceYear	Month	Amount
1	14	2008	1	25
1	14	2008	4	40
1	14	2008	8	14
1	15	2008	2	12
1	15	2008	3	25
1	15	2008	4	26
1	14	2009	5	67
1	14	2009	7	47
2	14	2008	8	14
2	15	2008	6	12
2	15	2009	4	12
				
Company_IDReferenceYear	Month	Amount
1	14	2008	1	25
1	14	2008	2	0
1	14	2008	3	0
1	14	2008	4	40
1	14	2008	5	0
1	14	2008	6	0
1	14	2008	7	0
1	14	2008	8	14
1	14	2008	9	0
1	14	2008	10	0
1	14	2008	11	0
1	14	2008	12	0
1	14	2009	1	0
1	14	2009	2	0
1	14	2009	3	0
1	14	2009	4	0
1	14	2009	5	67
1	14	2009	6	0
1	14	2009	7	47
1	14	2009	8	0
1	14	2009	9	0
1	14	2009	10	0
1	14	2009	11	0
1	14	2009	12	0
1	15	2008	1	0
1	15	2008	2	12
1	15	2008	3	25
1	15	2008	4	26
1	15	2008	5	0
1	15	2008	6	0
1	15	2008	7	0
1	15	2008	8	0
1	15	2008	9	0
1	15	2008	10	0
1	15	2008	11	0
1	15	2008	12	0
1	15	2009	1	0
1	15	2009	2	0
1	15	2009	3	0
1	15	2009	4	0
1	15	2009	5	0
1	15	2009	6	0
1	15	2009	7	0
1	15	2009	8	0
1	15	2009	9	0
1	15	2009	10	0
1	15	2009	11	0
1	15	2009	12	0

Answer : SQL Stored Procedure

this is what you are exactly looking for...
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:
37:
38:
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
Random Solutions  
 
programming4us programming4us