计算列和行的平均值

时间:2017-12-26 09:13:12

标签: sql sql-server average rating

下面是我的表格(tblReviewRating),其中包含数据:

rrFood    rrService    rrCleanliness    rrAmbience    rrBqtID    rrCusID
------    ---------    -------------    ----------    -------    -------
3         4            2                1             1          1
5         4            3                2             1          2
4         2            4                2             2          5
4         2            4                2             2          7
4         2            4                2             2          9

我正在尝试计算来自客户(rrBqtID)的每个独立宴会厅(rrCusID)的前四列(5个比例)的平均值:

SELECT rrFood,rrService,rrCleanliness,rrAmbience, 
       ROUND(AVG(rrFood+rrService+rrCleanliness+rrAmbience),2) AS AverageRating 
FROM tblReviewRating
GROUP BY rrFood,rrService,rrCleanliness,rrAmbience

如何计算客户提供的每个宴会厅的四项服务的平均评分和AverageRating的平均评分?

编辑: 我想在查询中应用this example来计算评分。

3 个答案:

答案 0 :(得分:2)

如果你想要rrBqtID,rrCusID的avg你应该为rrFood,rrService,rrCleanliness,rrAmbience分组,但对于rrBqtID,rrCusID

SELECT rrBqtID, rrCusID, 
       ROUND(AVG(rrFood+rrService+rrCleanliness+rrAmbience),2) AS AverageRating 
FROM tblReviewRating
GROUP BY rrBqtID, rrCusID

如果你想看到其他信息附近的平均值,你应该使用连接,例如;

SELECT A.rrFood,A.rrService,A.rrCleanliness,A.rrAmbience , T.AverageRating
FROM tblReviewRating A
INNER JOIN (

    SELECT rrBqtID, rrCusID, 
           ROUND(AVG(rrFood+rrService+rrCleanliness+rrAmbience),2) AS AverageRating 
    FROM tblReviewRating
    GROUP BY rrBqtID, rrCusID
) T ON T.rrBqtID = A.rrBqtID and T.rrCusID = A.rrCusID

答案 1 :(得分:1)

您不应group by汇总column,而应column group byrrFoodrrService因此,不应按rrCleanlinessrrAmbiencegroup byrrBqtID分组,而应分别rrCustID columnAVG

您可以使用公式

获得四个select rrBqtID, rrCustID, AVG(AverageRating) as AverageRating from (SELECT rrBqtID, rrCustID, ROUND(rrFood+rrService+rrCleanliness+rrAmbience) / 4,2) AS AverageRating FROM tblReviewRating) t group by rrBqtID, rrCustID 的平均值

(column1 + column2 + column3 + column4)/ 4

然后使用SELECT abr, @abr from table ^---- this doesn't exist, but instead of this I need to evaluate @S1.

对此进行汇总
mongoDB

答案 2 :(得分:1)

这可能会帮助您了解每个宴会厅的平均评分。希望这有帮助。

SELECT rrBqtID, AVG(rrFood) AS AVGrrFood,AVG(rrService) AS AVGrrService,AVG(rrCleanliness) AS AVGrrCleanliness,AVG(rrAmbience) AS AVGrrAmbience, 
   ROUND(AVG((rrFood+rrService+rrCleanliness+rrAmbience)/4),2) AS AverageRating 
FROM tblReviewRating
GROUP BY rrBqtID