计算列和行的平均值

Calculate average of columns and rows

下面是我的 table (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

我正在尝试从客户 (rrCusID) 计算每个单独宴会厅 (rrBqtID) 前四列(满分 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 来计算评分。

如果您想要 rrBqtID、rrCusID 的平均值,您应该对 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

你不应该group by你正在聚合的columns,而是你想要group bycolumns。因此,不是按 rrFoodrrServicerrCleanlinessrrAmbience 分组,您应该分别 group by rrBqtIDrrCustID .

然后用

的公式可以得到四个column的平均值

(column1 + column2 + column3 + column4) / 4

然后聚合这个,使用AVG:

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

这可能有助于您查看每个宴会厅的平均收视率。希望这有帮助。

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