我正在尝试按评级选择产品评论的数量,其中评级可以是 0 - 5。
以下基本选择有效,但不会给出基础表中不存在的评级计数。
SELECT Rating, COUNT(*) AS 'Reviews' FROM ProductReviews
WHERE ProductID = 'product1'
GROUP BY Rating
我尝试使用 CTE 生成缺失的结果,并使用外部连接加入评论表,只要我尝试包含“分组依据”表达式,结果就会回退以匹配基本查询的结果。
(我已经检查过 CTE 确实生成了所需值的全部范围)。
BEGIN
DECLARE @START AS INT = 0;
DECLARE @END AS INT = 5;
WITH CTE_Ratings AS
(
SELECT @START as cte_rating
UNION ALL
SELECT 1 + cte_rating
FROM CTE_Ratings
WHERE cte_rating < @END
)
SELECT
cte_rating AS 'ReviewRating'
, ISNULL(COUNT(*), 0) AS 'ReviewCount'
FROM CTE_Ratings
LEFT OUTER JOIN Reviews ON Reviews.Rating = cte_rating
WHERE ProductReviews.ProductID = 'product1'
AND cte_rating BETWEEN @START AND @END
GROUP BY cte_rating
END
(我还尝试构建一个包含所需值的临时表,连接到评论表,结果相同)。
在上述两个查询的情况下,结果是:
Rating Reviews
0 1
3 3
4 9
5 47
而我试图获得的相同数据是:
Rating Reviews
0 1
1 0
2 0
3 3
4 9
5 47
任何人都可以建议添加 Group By 聚合函数何时导致查询失败,或者如何改进它?