使用查询在sql server中查找平均值 [英] using query find the average in sql server
问题描述
使用查询我得到以下输出如下
费率Bfid
1 72
2 72
3 72
4 72
从上面的输出我希望输出如下
Rate Bfid
2.5 72
添加费率值1 + 2 + 3 + 4 = 10分为72'的分数。
所以我得到2.5
使用sql查询如何获得以下输出。
请帮帮我。
Rgds,
Narasiman P.
using query i get the below output as follows
Rate Bfid
1 72
2 72
3 72
4 72
From the above output i want the output as follows
Rate Bfid
2.5 72
adding the Rate values 1+2+3+4 = 10 divide bu number of 72''s .
so in rate i get the 2.5
from using sql query how can i get the below output.
please help me.
Rgds,
Narasiman P.
推荐答案
SELECT AVG(Rate), Bfid FROM tablename GROUP BY Bfid
编辑:如果Rate是int而不是decimal,则必须先将所有值转换为decimal,以便能够返回十进制值。如下:
in case Rate is int, and not decimal, you will have to cast all values to decimal first, in order to be able to return decimal value. Like bellow:
CREATE TABLE #Test (Rate int, Bfid int)
INSERT INTO #Test (Rate, Bfid) VALUES (1, 72)
INSERT INTO #Test (Rate, Bfid) VALUES (2, 72)
INSERT INTO #Test (Rate, Bfid) VALUES (3, 72)
INSERT INTO #Test (Rate, Bfid) VALUES (4, 72)
SELECT AVG(CAST(Rate as decimal)), Bfid
FROM #Test
GROUP BY Bfid
ORDER BY Bfid
DROP TABLE #Test
这篇关于使用查询在sql server中查找平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!