我有一个SELECT语句,根据年份和类别对评论进行排序。它正确显示了数据,但我可以做些什么才能显示年度评论总数和所有评论总数。
SELECT PRH.PRHYear AS 'Year',
C.CategoryName AS 'Category',
COUNT(*) AS 'Reviews'
FROM ProductRankingHistory PRH
RIGHT JOIN
Review R
ON PRH.PRHProduct = R.ProductID
RIGHT JOIN
Product P
ON R.ProductID = P.ProductID
RIGHT JOIN
Category C
ON P.CategoryID = C.CategoryID
WHERE R.ProductID = P.ProductID AND
P.CategoryID = C.CategoryID AND
C.CategoryID = PRH.PRHCategory
GROUP BY PRH.PRHYear, C.CategoryName
ORDER BY PRHYear ASC
答案 0 :(得分:0)
一年中的评论总数可以通过表达式count(*) over (partition by year)
计算得出。您可以直接将其放在ORDER BY
子句
SELECT PRH.PRHYear AS 'Year',
C.CategoryName AS 'Category',
COUNT(*) AS 'Reviews'
FROM ProductRankingHistory PRH
RIGHT JOIN
Review R
ON PRH.PRHProduct = R.ProductID
RIGHT JOIN
Product P
ON R.ProductID = P.ProductID
RIGHT JOIN
Category C
ON P.CategoryID = C.CategoryID
WHERE R.ProductID = P.ProductID AND
P.CategoryID = C.CategoryID AND
C.CategoryID = PRH.PRHCategory
GROUP BY PRH.PRHYear, C.CategoryName
ORDER BY PRHYear ASC, count(*) over (partition by year)