0
這是我的CustomerDetails表。獲取最近三個月的商店銷售發票數
CustomerID CustCodeID
25 1
65 8
35 2
112 8
45 2
975 8
364 1
48 8
69 1
97 8
33 1
11 8
93 2
10 8
21 1
65 8
74 2
53 8
這是我的Fact_SalesMetrics表。
Date Sales # CustomerID
2015-03-23 00:00:00.000 42895 25
2015-03-13 00:00:00.000 53920 53
2015-03-23 00:00:00.000 44895 65
2015-03-13 00:00:00.000 43920 35
2015-03-23 00:00:00.000 48895 112
2015-03-13 00:00:00.000 47920 45
2015-03-23 00:00:00.000 46895 975
2015-03-13 00:00:00.000 45920 48
2015-03-23 00:00:00.000 40895 69
2015-03-13 00:00:00.000 40920 11
2015-03-23 00:00:00.000 41895 33
2015-03-13 00:00:00.000 49920 21
......
我願輸出象下面這樣:
CustCodeID March 2015
1 4
2 2
8 7
這意味着誰擁有三月代碼ID「1」,有4下訂單的客戶,2有2個訂單,這樣的。
要做到這一點,我問類似下面並得到它的工作:
select CustCodeID,sum(March) as 'March 2015' from (
select bb.CustCodeID, aa.March from (
(SELECT count(distinct([Sales #])) as 'March', customerid
FROM [SalesData].[dbo].[Fact_SalesMetrics] a
where date >= '2015-03-01 00:00:00.000' and date <= '2015-03-31 00:00:00.000'
and customerid in (select customerid from CustomerDetails)
group by customerid) as aa inner join (select customerid,CustCodeID from CustomerDetails) as bb on aa.customerid=bb.customerid
)
) as dd group by CustCodeID
我現在想計算的發票數爲過去三個月象下面這樣:
CustCodeID March 2015 February 2015 January 2015
1 4 ? ?
2 2 ? ?
8 7 ? ?
任何人都可以幫我實現這個目標?
樞按月執行樞軸查詢。 – 2015-03-31 13:07:33
這是什麼意思?你能解釋一下查詢嗎? – 2015-03-31 13:22:12