SQL透视图中的月明智数据

时间:2018-07-10 11:44:13

标签: sql sql-server

我有一个包含以下数据的表。

Ref YEAR    MONTH   CONF_DATE   Active_Customer_Count   Role_name
43132_43252 2018    2   2/1/2018    492                 Call Center
43132_43252 2018    2   2/1/2018    113                 Others
43160_43252 2018    3   3/1/2018    1562                Call Center
43160_43252 2018    3   3/1/2018    394                 Others
43191_43252 2018    4   4/1/2018    1297                Call Center
43191_43252 2018    4   4/1/2018    517                 Others
43221_43252 2018    5   5/1/2018    2065                Call Center
43221_43252 2018    5   5/1/2018    679                 Others
43252_43252 2018    6   6/1/2018    4470                Call Center
43252_43252 2018    6   6/1/2018    662                 Others

我希望它采用以下格式。

2018    
2   605
3   1956
4   1814
5   2744
6   5132
2018 Total  12251

这可以通过sqlivot完成吗?

我已经做到了,但是输出不是每个月一次。

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.[month]) 
            FROM #Active2 c 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'SELECT year, ' + @cols + ' from 
            (
                select*
                from #Active2 
           ) x
            pivot 
            (
                 SUM(Active_Customer_Count)
                for [month] in (' + @cols + ') 
            ) p '
execute(@query)

输出:

year    2   3   4   5   6
2018    492 NULL    NULL    NULL    NULL
2018    113 NULL    NULL    NULL    NULL
2018    NULL    1562    NULL    NULL    NULL
2018    NULL    394 NULL    NULL    NULL
2018    NULL    NULL    1297    NULL    NULL
2018    NULL    NULL    517 NULL    NULL
2018    NULL    NULL    NULL    2065    NULL
2018    NULL    NULL    NULL    679 NULL
2018    NULL    NULL    NULL    NULL    4470
2018    NULL    NULL    NULL    NULL    662

1 个答案:

答案 0 :(得分:1)

尝试一下

IF OBJECT_ID('tempdb..#t')IS NOT NULL
DROP TABLE #t

;WITH CTE(Ref ,[YEAR],[MONTH], CONF_DATE,  Active_Customer_Count, Role_name)
AS
(
SELECT '43132_43252',2018, 2,'2/1/2018', 492  ,'Call Center' UNION ALL
SELECT '43132_43252',2018, 2,'2/1/2018', 113  ,'Others'      UNION ALL
SELECT '43160_43252',2018, 3,'3/1/2018', 1562 ,'Call Center' UNION ALL
SELECT '43160_43252',2018, 3,'3/1/2018', 394  ,'Others'      UNION ALL
SELECT '43191_43252',2018, 4,'4/1/2018', 1297 ,'Call Center' UNION ALL
SELECT '43191_43252',2018, 4,'4/1/2018', 517  ,'Others'      UNION ALL
SELECT '43221_43252',2018, 5,'5/1/2018', 2065 ,'Call Center' UNION ALL
SELECT '43221_43252',2018, 5,'5/1/2018', 679  ,'Others'      UNION ALL
SELECT '43252_43252',2018, 6,'6/1/2018', 4470 ,'Call Center' UNION ALL
SELECT '43252_43252',2018, 6,'6/1/2018', 662  ,'Others'
)
SELECT * INTO #T FROM CTE

SELECT * FROM
(
SELECT  [YEAR],
        ISNULL(CAST([MONTH] AS VARCHAr(10)),'Total') AS [MONTH],
        SUM(Active_Customer_Count) Active_Customer_Count 
FROM #T
GROUP BY [YEAR],[MONTH]
WITH ROLLUP
)dt 
WHERE [YEAR] IS NOT NULL

演示结果:http://rextester.com/PNRDX57012