获取过去三个月商店的发票数量

时间:2015-03-31 13:05:10

标签: sql sql-server

这是我的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

这意味着拥有codeID' 1' 3月份有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                ?                   ?

任何人都可以帮我实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

由于您拥有DateTime类型的列,因此需要将其转换为Month Year格式。在Sql Server中使用DateName来提取月份名和年份。然后,您应该使用新的日期格式和CustomerID找到CustCodeIDGROUP BY的计数。您应该使用此查询作为要转动的表的源查询。

如果预先知道月份的值,则可以通过对列名称进行硬编码来使用静态数据透视

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

如果事先不知道列数,您可以选择动态数据透视

为此,第一步是在从行进行透视后显示列名。在以下查询中,它将选择过去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('+@cols+')
            ) p            
            ORDER BY CustCodeID;'     

EXEC SP_EXECUTESQL @query