2015-03-31 70 views
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    ?     ? 

任何人都可以幫我實現這個目標?

+2

樞按月執行樞軸查詢。 – 2015-03-31 13:07:33

+0

這是什麼意思?你能解釋一下查詢嗎? – 2015-03-31 13:22:12

回答

0

由於您有類型DateTime的列,因此您需要將其轉換爲Month Year格式。在Sql Server中使用DateName來提取月份名稱和年份。然後您應該找到新日期格式的CustomerIDGROUP BY以及CustCodeID。您應該使用此查詢作爲要轉化的表的源查詢。

如果個月的值是預先知道的,你可以通過硬編碼的列名

SELECT CustCodeID,[March 2015],[February 2015],[January 2015] 
FROM 
(
    SELECT CD.CustCodeID,COUNT(CD.CustomerID) COUNTOfCustomerID, 
    DATENAME(MONTH,[DATE])+' ' + DATENAME(YEAR,[DATE]) MONTHS 
    FROM CustomerDetails CD 
    JOIN Fact_SalesMetrics FS ON CD.CustomerID=FS.CustomerID 
    GROUP BY CD.CustCodeID,DATENAME(MONTH,[DATE])+' ' + DATENAME(YEAR,[DATE]) 
)TAB 
PIVOT 
(
    MIN(COUNTOfCustomerID) 
    FOR MONTHS IN([March 2015],[February 2015],[January 2015]) 
)P 

如果數使用靜態數據透視列不提前知道,你可以去Dynamic Pivot

爲此,第一步是從行中獲取要顯示的列名稱。在以下查詢中,它將選擇最近3個月的列。

DECLARE @cols NVARCHAR (MAX) 

SELECT @cols = STUFF((SELECT ',' + QUOTENAME(MONTHS) 
      FROM 
      (
       SELECT TOP 3 DATENAME(MONTH,[DATE])+' ' + DATENAME(YEAR,[DATE]) MONTHS 
       from Fact_SalesMetrics 
       GROUP BY '01 ' + DATENAME(MONTH,[DATE])+' ' + DATENAME(YEAR,[DATE]), 
       DATENAME(MONTH,[DATE])+' ' + DATENAME(YEAR,[DATE]) 
       ORDER BY CAST('01 ' + DATENAME(MONTH,[DATE])+' ' + DATENAME(YEAR,[DATE]) AS DATE) DESC 
      ) c 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

現在使用Dynamic sql

DECLARE @query NVARCHAR(MAX) 
SET @query = ' 
      SELECT * FROM 
      (
       SELECT CD.CustCodeID,COUNT(CD.CustomerID) COUNTOfCustomerID, 
       DATENAME(MONTH,[DATE])+'' '' + DATENAME(YEAR,[DATE]) MONTHS 
       FROM CustomerDetails CD 
       JOIN Fact_SalesMetrics FS ON CD.CustomerID=FS.CustomerID 
       GROUP BY CD.CustCodeID,DATENAME(MONTH,[DATE])+'' '' + DATENAME(YEAR,[DATE]) 
      ) x 
      PIVOT 
      (
       -- Specify the values to hold in pivoted column 
       MIN([COUNTOfCustomerID]) 
       -- Get the column names from variable 
       FOR [MONTHS] IN('[email protected]+') 
      ) p    
      ORDER BY CustCodeID;'  

EXEC SP_EXECUTESQL @query