SQL Query不会执行。无法执行聚合功能

时间:2017-08-28 13:15:21

标签: sql sql-server

我已经看到有关此问题的其他帖子,但目前尚不清楚如何解决此问题。

我相信最后一次更新声明就是问题所在。我正在尝试更新RatingValue,这是评级之和除以评级数。

DELETE
FROM EasyDNNNewsComments
WHERE CommentID = 168

DELETE
FROM ArticleRating
WHERE ArticleID = 6605 AND UserID = 10

UPDATE EasyDNNNews 
SET NumberOfComments = (NumberOfComments - 1)
WHERE ArticleID = 6605 

UPDATE EasyDNNNews 
SET RatingValue = SUM((SELECT RatingValue 
                       FROM ArticleRating 
                       WHERE ArticleID = 6605)) / 
                     (SELECT COUNT(*) 
                      FROM ArticleRating 
                      WHERE ArticleID = 6605)

2 个答案:

答案 0 :(得分:5)

您不需要为此使用多个查询。只需使用聚合作为数学值。

UPDATE EasyDNNNews 
    SET RatingValue = (SELECT SUM(RatingValue) / COUNT(*) FROM ArticleRating WHERE ArticleID = 6605)
where ArticleID = 6605

答案 1 :(得分:1)

试试这个:

UPDATE EasyDNNNews 
    SET RatingValue = (SELECT SUM(RatingValue)/COUNT(*) FROM ArticleRating WHERE ArticleID = 6605)
    WHERE ArticleID = 6605;