1

我第一次尝试使用 PIVOT,但我无法让它工作,我似乎无法弄清楚我做错了什么。

我从一个讨厌的 select 语句开始。当我得到这些结果时,我可以得到结果,我一直在做的是导出到 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 的名称。

4

1 回答 1

0

我想而不是

IN (#tmp1.ProviderName)

你需要

IN ([element1],[element2],[element3], ... )

element*n* 是#tmp1.ProviderName 内容的所有变体。在最顶部的 SELECT 部分中,您必须将这些元素列为列

您应该为这两个表语句提供不同的别名,那里有两次 #tmp1。

首先尝试一个简单的 PIVOT 测试示例,其中列少得多。如果你弄清楚它是非常强大的。我喜欢 excel 中的数据透视功能(数据透视图!),但数据库可以更快地进行聚合。

于 2013-09-13T23:11:46.703 回答