**注意:我需要进一步添加 NULLIF(0 或 5)。我在这里写了一篇关于我的回答的简短文章:http: //peterkellner.net/2013/10/13/creating-a-compound-nullif-in-avg-function-with-sqlserver/ 但对我的解决方案不满意)
我有一张结果表,与会者在其中输入课程的预计出勤率。如果他们输入 0 或将其留空,我想忽略它并获取输入值的平均值。如果没有整个 SQL 的 where 子句,我无法弄清楚如何将该约束添加到我的 AVG 函数中。那可能吗?我的代码如下所示:(EstimatedNumberAttendees 是我要追求的)。
SELECT dbo.SessionEvals.SessionId,
AVG(Cast (dbo.SessionEvals.CourseAsWhole as Float)) AS CourseAsWholeAvg,
COUNT(*),
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'On Time' then 1
else null
end) AS SpeakerOnTime,
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'Late' then 1
else null
end) AS SpeakerLate,
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'NoShow' then 1
else null
end) AS SpeakerNoShow,
COUNT(case
when dbo.SessionEvals.PercentFull = '10% to 90%' then 1
else null
end) AS PercentFull10to90,
COUNT(case
when dbo.SessionEvals.PercentFull = '> 90%' then 1
else null
end) AS PercentFullGreaterThan90,
COUNT(case
when dbo.SessionEvals.PercentFull = ' < 10% Full ' then 1
else null
end) AS PercentFullLessThan10,
AVG(Cast (dbo.SessionEvals.EstimatedNumberAttendees as Float)) AS
EstimatedAttending
FROM dbo.Sessions
INNER JOIN dbo.SessionEvals ON (dbo.Sessions.Id =
dbo.SessionEvals.SessionId)
WHERE dbo.Sessions.CodeCampYearId = 8
GROUP BY dbo.SessionEvals.SessionId