我第一次尝试使用PIVOT而且我无法让它工作,我似乎无法弄清楚我做错了什么。
我从一个讨厌的选择语句开始。当我得到这些结果时,我可以得到结果,我一直在做的是导出到Excel并使用其中的数据透视功能。当我将数据导入Excel时,我会将其作为我的操作:http://imgur.com/N024rEi
然后我进入下一步并尝试在SQL中完成整个数据透视但无法使其工作。这是我的代码:
SELECT DISTINCT #tmp1.AuthID
, #tmp1.ProviderID
, #tmp1.ProviderName
, #tmp1.LOCID
, #tmp1.LOCDesc
, '$' + CONVERT(VARCHAR,CAST(#tmp1.TotAuthAmt AS MONEY),-1) AS 'ToAuthAmt'
, CONVERT(VARCHAR(10), #tmp1.AuthDate, 101) AS AuthDate,
CONVERT(VARCHAR(10), #tmp1.AuthExpirationDate, 101) AS AuthExpirationDate
, DATEDIFF(D,#tmp1.AuthDate,#tmp1.AuthExpirationDate) AS AuthLenInDays
, (CASE WHEN
ISNULL(#tmp2.TotPaidAmt, 0) = 0 THEN '$0.00'
ELSE '$' + CONVERT(VARCHAR,CAST(#tmp2.TotPaidAmt AS MONEY),-1)
END) AS TotPaidAmt,
#tmp1.NbrOfAuthorizations
, #tmp1.UnitsAuthorized
, #tmp1.RatePerUnit
, #tmp1.CAREMODE
, (CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END) AS AuthUnitsClaimed
, (#tmp1.UnitsAuthorized -
(CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END)) AS UnclaimedAuths
, (CASE WHEN
ISNULL(#tmp2.ExpiredAuths, 0) = 0 THEN 0
ELSE 1
END) AS ExpiredIndicator
FROM (SELECT
DISTINCT #tmp1.AuthID
, #tmp1.ProviderID
, #tmp1.ProviderName
, #tmp1.LOCID
, #tmp1.LOCDesc
, '$' + CONVERT(VARCHAR,CAST(#tmp1.TotAuthAmt AS MONEY),-1) AS 'ToAuthAmt'
, CONVERT(VARCHAR(10), #tmp1.AuthDate, 101) AS AuthDate
, CONVERT(VARCHAR(10), #tmp1.AuthExpirationDate, 101) AS AuthExpirationDate
, DATEDIFF(D,#tmp1.AuthDate,#tmp1.AuthExpirationDate) AS AuthLenInDays
, (CASE WHEN
ISNULL(#tmp2.TotPaidAmt, 0) = 0 THEN '$0.00'
ELSE '$' + CONVERT(VARCHAR,CAST(#tmp2.TotPaidAmt AS MONEY),-1)
END) AS TotPaidAmt,
#tmp1.UnitsAuthorized
, #tmp1.RatePerUnit
, #tmp1.CAREMODE
, (CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END) AS AuthUnitsClaimed
, (#tmp1.UnitsAuthorized -
(CASE WHEN
ISNULL(#tmp2.UnitsAdjudicated, 0) = 0 THEN 0
ELSE #tmp2.UnitsAdjudicated
END)) AS UnclaimedAuths
, (CASE WHEN
ISNULL(#tmp2.ExpiredAuths, 0) = 0 THEN 0
ELSE 1
END) AS ExpiredIndicator
FROM #tmp1
LEFT JOIN #tmp2
ON #tmp2.AuthID = #tmp1.AuthID
GROUP BY #tmp1.AuthID, #tmp1.ProviderID, #tmp1.LOCID, #tmp1.LOCDesc, #tmp1.UnitsAuthorized, #tmp1.RatePerUnit, #tmp1.FamilyID, #tmp1.ProviderName, #tmp1.TotAuthAmt,
#tmp1.AuthDate, #tmp1.AuthExpirationDate, #tmp2.TotPaidAmt, #tmp1.NbrOfAuthorizations, #tmp1.CAREMODE, #tmp2.UnitsAdjudicated, #tmp2.ExpiredAuths
)#tmp1
PIVOT (
SUM(#tmp1.NbrOfAuthorizations) FOR ProviderName
IN (#tmp1.ProviderName)
) AS #tmp1
我需要在此处更改才能使其正常工作?另外,我如何添加屏幕截图中显示的其他项目?
EDIT1:看起来我在使用PIVOT之后能够获得结果:
PIVOT
(SUM(NbrOfAuthorizations) FOR [ProviderName] IN (element1, element2, etc.)
) PivotResults1
PIVOT
(AVG(AuthLenInDays) FOR [CAREMODE] IN ([element1])
) PivotResults2
但仍有一些问题...... SUM语句实际上似乎并未将特定值的值汇总到一列中。我看到每个单元格都有一个'1'或NULL,如下例所示:http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query。另一件事是我的AVG专栏,我无法弄清楚如何更改该列的名称,它始终是element1的名称。
答案 0 :(得分:0)
IN (#tmp1.ProviderName)
你需要
IN ([element1],[element2],[element3], ... )
元素* n *是#tmp1.ProviderName内容的所有变体。在最顶部的SELECT部分中,您必须将这些元素列为列
和
你应该给两个表语句赋予不同的别名,你在那里有#tmp1两次。
首先尝试一个简单的PIVOT测试示例,其中列数少得多。如果你弄清楚它是非常强大。我喜欢excel(pivotcharts!)中的数据透视功能,但数据库可以更快地进行聚合。