[英]Row and column total in dynamic pivot in MS SQL Server 2008 my questions is row total and columns total) for all products when add
[英]How can I divide my pivot columns by the total across the row?
我有以下查詢,我幾乎在(我認為)需要獲取它的地方。 但是,下圖中的總和需要除以各行的總計數,才能顯示為百分比,而不是原始計數。
WITH cteServices ( [Cnt], OrgName, CatName )
AS ( SELECT DISTINCT
COUNT(DISTINCT [S].[ServiceId]) AS 'EBPServices' ,
[O].[OrganizationName] ,
cref.CSAPCategoryName
FROM DVSdmPA.[DM].[Organizations] AS O
LEFT JOIN DVSdmPA.[DM].[Programs] AS P ON [O].[OrganizationID] = [P].[OrganizationId]
LEFT JOIN DVSdmPA.[DM].[Services] AS S ON [P].[ProgramId] = [S].[ProgramId]
AND [S].[FiscalYear] IN (
CAST('Jul 1 2012 12:00AM' AS DATETIME) )
INNER JOIN DVSdmPA.[MDM_ST].[CountySCALink] AS CSCAL ON [O].[OrganizationId] = [CSCAL].[OrganizationID]
INNER JOIN DVSdmPA.[MDM_ST].[SCAReference] AS SCAR ON [CSCAL].[CountyID] = [SCAR].[CountyID]
INNER JOIN DVSMDM.GIS.CountyBoundaries CB ON CSCAL.CountyID = CB.CountyId
INNER JOIN DVSdmPA.MDM.CSAPCategoryReference cref ON s.CSAPCategoryCode = cref.CSAPCategoryId
WHERE [O].[OrganizationName] NOT IN ( 'National Guard',
'BDAP Problem Gambling Prevention Pilot',
'Clarion' )
AND s.CSAPCategoryCode IS NOT NULL
GROUP BY [O].[OrganizationName] ,
cref.CSAPCategoryName
)
SELECT OrgName ,
ISNULL(Alternatives, 0) AS Alternatives ,
ISNULL([Community-Based Process], 0) AS [Community-Based Process] ,
ISNULL(Education, 0) AS Education ,
ISNULL(Environmental, 0) AS Environmental ,
ISNULL([Information Dissemination], 0) AS [Information Dissemination] ,
ISNULL([Problem Identification and Referral], 0) AS [Problem Identification and Referral]
FROM ( SELECT cnt ,
OrgName ,
CatName
FROM cteServices
) src PIVOT ( SUM(cnt) FOR src.CatName IN ( Alternatives,
[Community-Based Process],
Education,
Environmental,
[Information Dissemination],
[Problem Identification and Referral] ) ) AS result
ORDER BY orgname;
一個簡單的解決方法是簡單地將每一行除以總和,就像這樣(難看,但是可以):
ISNULL(Alternatives, 0) / (ISNULL(Alternatives, 0) + ISNULL([Community-Based Process], 0) + ISNULL(Education, 0) + ISNULL(Environmental, 0) + ISNULL([Information Dissemination], 0) + ISNULL([Problem Identification and Referral], 0)) as AlternativesPercent
您還可以使用WHERE
和GROUP BY
在子查詢中計算總和-相同的想法,只是稍微干凈一點。
更重要的是,您應該考慮是否真的要合並這樣的數據。 在您提供的圖像中,當有人問“讓我看看貝德福德的5個環境”時,會發生什么—您需要運行另一個查詢。 如果更進一步,提供百分比而不是名義數字,您會遇到更多問題,例如“更大的是20%的Allegheny還是60%的Blair?”。 諸如Excel之類的工具或任何商業智能程序,都可以輕松處理原始數據,並非常輕松地創建您在此處難以創建的數據透視表。 與在SQL Server中相比,在Excel中將百分比格式精確地設置為精確到小數點后1位的其他操作要容易得多。 Excel和其他工具支持追溯...您可以單擊Bedford的5 Environmentals,它們將立即顯示為列表。 因此,您將兩全其美-整合的數據透視表和原始數據。 SQL僅需要其他查詢即可獲取基礎數據行。
考慮所有選項。
我的建議並非最有效,但我認為應該可行:
將最終選擇設置為cte(YourFinalSelectAsCte)
添加另一個帶有選擇的CTE
,MyCTE as
(
select
OrgName,
(Alternatives+
[Community-Based Process]
Education+
Environmental+
[Information Dissemination]+
[Problem Identification and Referral]) as SummedRow
from YourFinalSelectAsCte
)
然后只需在OrgName上的YourFinalSelectAsCte上加入MyCTE,然后將每個條目除以SummedRow
同樣,我在OrgName上的聯接不是最有效的,應該在先前ctes的選擇中重復ServiceId並將其用作聯接。
希望我能正確理解您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.