我希望生成的表格類似於我提供的示例表,即使提供的唯一月份是5,我希望表格顯示所有12個月,即使金額爲在其他月份爲0。處理日期和總結行數
我正在使用這個查詢,讓我嘗試和解釋到底是什麼,當談到金額。我希望金額欄包含相應月份的每筆銷售額的總和。
Select
YEAR([Date]) as [Year],
MONTH([Date]) as [Month],
IsNull(SUM(Amount), 0) AS TotalSales
From Sales Left Outer Join Employee
On Employee.EmployeeID = Sales.EmployeeID
Group By YEAR([Date]), MONTH([Date])
Order By YEAR([Date]), MONTH([Date])
Employee表,
+--+--+------+
|ID| Name |
+--+---------+
|1 |John Doe |
+--+---------+
|2 |Jane Doe |
+--+---------+
銷售表,
+--+------+---------+-------+--------+
|ID|SaleID| Date |Amount |Quantity|
+--+------+---------+-------+--------+
|1 | 1 |5-14-2014|300 |12 |
+--+------+---------+-------+--------+
|1 | 2 |5-16-2014|600 |4 |
+--+------+---------+-------+--------+
|2 | 3 |5-14-2014|452 |10 |
+--+------+---------+-------+--------+
|2 | 4 |5-16-2014|356 |2 |
+--+------+---------+-------+--------+
我想要什麼,
+--+---------+-----+------+--------+--------+
|ID| Name |Year |Month |Amount |Quantity|
+--+---------+-----+------+--------+--------+
|1 |John Doe |2014 |5 |900 |16 |
+--+---------+-----+------+--------+--------+
|2 |Jane Doe |2014 |5 |808 |12 |
+--+---------+-----+------+--------+--------+
似乎是工作:)不過關於我的其它要求,我想對結果顯示所有個月,即使他們沒有爲出售日上市。 – Prosperity
您可以編輯您的問題,並提供樣品輸出@Prosperity –
基本上與您的輸出,但本月列中,將顯示該月(1-12),即使該月將有0 – Prosperity