SQL语句中数据透视表中的计算字段计算不正确

时间:2019-05-09 19:31:17

标签: sql sql-server pivot-table

在我的SQL代码中,我创建了一个PIVOT表来更改列。我试图根据我创建的数据透视表列添加2个计算列,分别称为PendingEffectiveness和ClosingRatio,但由于我得到的全部为零,因此计算不正确。这是我的SQL代码:

-- Code for Summary Page
SELECT AdjusterName
    ,COALESCE(BeginningPending,0) as BeginningPending
    ,COALESCE(TransferredIn,0) as TransferredIn
    ,COALESCE(TransferredOut,0) as TransferredOut 
    ,COALESCE(FeaturesPending,0) as FeaturesPending 
    ,COALESCE(NewFeatures,0) as NewFeatures
    ,COALESCE(ClosedFeatures,0) as ClosedFeatures
    ,COALESCE(BeginningPending / FeaturesPending,0) * 100 as PendingEffectiveness
    ,COALESCE(ClosedFeatures / NewFeatures,0) / 100 as ClosingRatio
FROM 
(
    SELECT AdjusterName, [Type], COALESCE(FeatureCount,0) as FeatureCount
    FROM [dbo].[RPT_FeaturesByLitigationAdjuster]
    GROUP BY AdjusterName, Type, FeatureCount
) as SourceTable 
PIVOT (SUM([FeatureCount]) FOR [Type] in ([BeginningPending], [TransferredIn], [TransferredOut], [FeaturesPending], [NewFeatures], [ClosedFeatures])) as PivotTable;

这是我的查询结果集:

AdjusterName BeginningPending TransferredIn TransferredOut 
FeaturesPending NewFeatures ClosedFeatures PendingEffectiveness ClosingRatio

dstallings  51  2   0   53  0   57  0   0
jstrother   28  164 3   36  0   56  0   0
lfaulkner   247 23  8   259 22  94  0   0
rpatton     197 32  12  231 30  59  0   0

0 个答案:

没有答案