透视多个聚合列

时间:2016-10-13 19:13:38

标签: sql-server pivot-table

我想调整多个聚合列。 这是查询:

SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
        COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
       SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
       SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompleted,  -- Surveys_Completed flag in view is 1
       SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
       SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmail,
       SUM(CASE WHEN Source = 'Call' THEN 1 ELSE 0 END) AS IssueLoggedCall,
       SUM(CASE WHEN Source = 'Internal' THEN 1 ELSE 0 END) AS IssueLoggedInternal, 
       SUM(CASE WHEN Source = 'Chat' THEN 1 ELSE 0 END) AS IssueLoggedChat,
       CAST(ROUND(CONVERT(decimal(3,1), (SUM(CASE WHEN Board_Name = 'Application Support' THEN 1 ELSE 0 END) * 100.00)/COUNT(TicketNbr)), 2) AS INT) AS PercentCallsHandledAppSup,
       CAST(ROUND(CONVERT(decimal(3,1), (SUM(CASE WHEN Board_Name = 'IT Services' THEN 1 ELSE 0 END) * 100.00)/COUNT(TicketNbr)), 2) AS INT) AS PercentCallsHandledITServices
FROM v_rpt_service vsrv LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID  
WHERE vsrv.company_name <> 'XYZ Test Company' AND vsrv.date_entered BETWEEN @StartDate AND @EndDate
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber

部分结果是:

WkNumber    OpenedIssues    ClosedIssues    SurveysCompleted    
1               2               2                     0 
2               950            938                    29    

我想将表格转移到

                        Wk1     Wk2    Wk3 ...  
OpenedIssues              2      950
ClosedIssues              2      938
SurveysCompleted          0      29

我读到我必须先将桌子翻转然后转动 我创建了这个查询:

SELECT *
FROM
(
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
        COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
       SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
       SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise,  -- Surveys_Completed flag in view is 1
       SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
       SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmail,
       SUM(CASE WHEN Source = 'Call' THEN 1 ELSE 0 END) AS IssueLoggedCall,
       SUM(CASE WHEN Source = 'Internal' THEN 1 ELSE 0 END) AS IssueLoggedInternal, 
       SUM(CASE WHEN Source = 'Chat' THEN 1 ELSE 0 END) AS IssueLoggedChat,
       SUM(CASE WHEN Source = 'Activity Capture' THEN 1 ELSE 0 END) AS IssueLoggedActivityCapture,
       SUM(CASE WHEN Source IS NULL THEN 1 ELSE 0 END) AS IssueLoggedNotDefined,
       SUM(CASE WHEN entered_by in ('VStanizzo', 'MHoude') THEN 1 ELSE 0 END) AS IssuesCallsHandledByReception,
       SUM(CASE WHEN entered_by not in ('VStanizzo', 'MHoude') THEN 1 ELSE 0 END) AS IssuesCallsHandledBySupport,
       CAST(ROUND(CONVERT(decimal(3,1), (SUM(CASE WHEN Board_Name = 'Application Support' THEN 1 ELSE 0 END) * 100.00)/COUNT(TicketNbr)), 2) AS INT) AS PercentCallsHandledAppSup,
       CAST(ROUND(CONVERT(decimal(3,1), (SUM(CASE WHEN Board_Name = 'IT Services' THEN 1 ELSE 0 END) * 100.00)/COUNT(TicketNbr)), 2) AS INT) AS PercentCallsHandledITServices
FROM v_rpt_service vsrv LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID  
WHERE vsrv.company_name <> 'XYZ Test Company' AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)

) AS SourceTbl
UNPIVOT
(
    SvrCnts for CtnCol 
        in (OpenedIssues, ClosedIssues, SurveysCompletedWithConnectWise, IssueLoggedPortal, IssueLoggedEmail, IssueLoggedCall,
            IssueLoggedInternal, IssueLoggedChat, IssueLoggedActivityCapture, IssueLoggedNotDefined, IssuesCallsHandledByReception,
            IssuesCallsHandledBySupport, PercentCallsHandledAppSup, PercentCallsHandledITServices)
) AS UnPivotTbl
ORDER BY WkNumber

现在如何从这里转动?

0 个答案:

没有答案