SQL中的平均值和大小写 [英] Average and Case in SQL
本文介绍了SQL中的平均值和大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试生成报告,并且以下代码未产生预期的结果,这使我得到2行而不是一行.
I am trying to generate a report and the following code does not produce the desired results which gives me 2 lines rather than one.
ScoreTypeID的值可以为22、52、3或4.如果是22或52,我需要平均值,如果不是,我需要显示0.任何想法可能是什么问题?谢谢.
The ScoreTypeID could have values of 22, 52, 3 or 4 . if it is 22 or 52, I need the average and if not I need to show 0. Any idea what may be the problem ? Thanks.
CASE WHEN FAS1.ScoreTypeID = 22 THEN avg(fas1.totalscore)
WHEN FAS1.ScoreTypeID = 52 THEN avg(fas1.totalscore) ELSE 0
END AS 'Total Score',
推荐答案
我认为在您的完整查询中,您缺少GROUP BY
子句,例如
I think in your full query, you are missing the GROUP BY
clause, eg
SELECT ...
FROM ....
WHERE ..
GROUP BY FAS1.ScoreTypeID
这篇关于SQL中的平均值和大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文