我有一个有 3 列的表:EffectiveDate
, ActualStatus
, PredictedStatus
.
我需要分组依据EffectiveDate
并计算ActualStatus
列和PredictedStatus
列的每个状态的数量。
在sql中我会这样做:
select EffectiveDate,
ActualStatus,
SUM(case when ActualStatus = 'Bound' then 1
when ActualStatus = 'Declined' then 1
when ActualStatus = 'Quoted' then 1
when ActualStatus = 'Not Taken Up' then 1
when ActualStatus = 'Indication' then 1
when ActualStatus = 'Submitted' then 1
when ActualStatus = 'Lost' then 1
when ActualStatus = 'Indicated' then 1
else 0 end) as CountActual,
PredictedStatus,
SUM(case when PredictedStatus = 'Bound' then 1
when PredictedStatus = 'Not Bound' then 1
ELSE NULL end) as CountPredicted
from #Test
group by
EffectiveDate,
ActualStatus,
PredictedStatus
结果应如下所示: