invoice_id invoice_date
------------ --------------
1 2013-11-27
2 2013-10-09
3 2013-09-12
訂單表
order_id invoice_id product quantity total
--------- ---------- --------- --------- -------
1 1 Product 1 100 1000
2 1 Product 2 50 200
3 2 Product 1 40 400
4 3 Product 2 50 200
,我想一個簡單的SQL查詢產生以下結果
products Month 9 Total Month 10 Total Mont 11 Total
-------- ------------- -------------- -------------
Product 1 0 400 100
Product 2 200 0 200
我有試過以下sql查詢
SELECT orders.products, DATEPART(Year, invoices.invoice_date) Year, DATEPART(Month, invoices.invoice_date) Month, SUM(orders.total) [Total],
FROM invoices INNER JOIN orders ON invoices.invoice_id=orders.invoice_id
GROUP BY orders.products, DATEPART(Year, invoices.invoice_date), DATEPART(Month, invoices.invoice_date)
但它什麼也沒有返回。是否有可能得到這個結果與單個查詢,我該怎麼做?由於