您可以SUM根据自己的查询获取每个商店的,但您必须添加GROUP BY如下:
SELECT Storeid, COUNT(StoreId) as TotalReview,
SUM(Case OnTimeDelivery WHEN 'Within Time' THEN 1 ELSE 0 END) as OnTimeDeliveryWithinTime,
SUM(Case OnTimeDelivery WHEN 'Little Delay' THEN 1 ELSE 0 END) as OnTimeDeliveryLittleDelay,
SUM(Case OnTimeDelivery WHEN 'Excess Delay' THEN 1 ELSE 0 END) as OnTimeDeliveryExcessDelay
FROM StoreReviews
GROUP BY Storeid
现在,如果您想获得平均值,而不是每个商店,而是所有商店,并且基于您OnTimeDelivery是否Within Time可以这样使用:Little DelayExcess DelayAVG
SELECT CONVERT(Decimal(5,2),AVG(CAST(TotalReview as float))) as AvgTotal, CONVERT(Decimal(5,2),AVG(CAST(OnTimeDeliveryWithinTime as float))) as AvgWithinTime, CONVERT(Decimal(5,2),AVG(CAST(OnTimeDeliveryLittleDelay as float))) as AvgLittleDelay, CONVERT(Decimal(5,2),AVG(CAST(OnTimeDeliveryExcessDelay as float))) as AvgExcessDelay
FROM (
SELECT Storeid, COUNT(StoreId) as TotalReview,
SUM(Case OnTimeDelivery WHEN 'Within Time' THEN 1 ELSE 0 END) as OnTimeDeliveryWithinTime,
SUM(Case OnTimeDelivery WHEN 'Little Delay' THEN 1 ELSE 0 END) as OnTimeDeliveryLittleDelay,
SUM(Case OnTimeDelivery WHEN 'Excess Delay' THEN 1 ELSE 0 END) as OnTimeDeliveryExcessDelay
FROM StoreReviews
GROUP BY Storeid
) StoreAverages
看我的小提琴演示