支払いと顧客の2つのテーブルがあります。
1、2、3、4、5 の 5 種類のお客様がいます。
特定の年の各月の顧客タイプ (1,2,3,4,5) ごとの合計支払い額を取得したいと考えています。
また、いずれかの顧客タイプに支払いがない場合は、0 にする必要があります。
以下は私の現在のクエリです:-
SELECT
"Month" = month(o.PaymentDate)
, "Year" = year(o.PaymentDate)
, Amount = sum(o.Amount)
, c.CustomerTypeID
FROM
Payments o
INNER JOIN
Customers c ON o.CustomerID = c.CustomerID
WHERE
o.PaymentDate >= convert(DATETIME, '1/1/2013 12:00:00 AM')
AND o.PaymentDate < convert(DATETIME, '12/31/2013 12:00:00 AM')
GROUP BY
month(o.PaymentDate)
, year(o.PaymentDate)
,c.CustomerTypeID
ORDER BY
year(o.PaymentDate)
, month(o.PaymentDate)
,c.CustomerTypeID
そして結果は:-
月 年 金額 CustomerTypeID 1 2013 456 1 1 2013 678 2 1 2013 346 3 1 2013 3245 5
現在、CustomerType 4 のデータが提供されていないため、金額列に 0 を表示したいので、月と年は同じになります。
どんな体でも私を助けることができますか?
前もって感謝します。