我有SELECT
声明,根据评论的平均评分对评论进行评论和排名。我使用DENSE_RANK()
并且它正常工作,它按降序排列。我希望通过将它按年份分开来改进声明,因此2016年等级变为1,2,3,4等等,当它转向2017年时,它将从1再次重新开始。
SELECT
PRHYear AS 'Year',
C.CategoryName AS 'Category',
M.ManufacturerName AS 'Manufacturer',
P.ProductName AS 'Product',
PRHAverageRating AS 'Average Rating',
DENSE_RANK() OVER (ORDER BY PRHAverageRating DESC) AS 'Rank'
FROM
ProductRankingHistory PRH
RIGHT JOIN
Category C ON PRH.PRHCategory = C.CategoryID
RIGHT JOIN
Manufacturer M ON PRH.PRHManufacturer = M.ManufacturerID
LEFT JOIN
Product P ON PRH.PRHProduct = P.ProductID
ORDER BY
PRH.PRHYear ASC, PRH.PRHAverageRating DESC
答案 0 :(得分:0)
将PRHYear的分区添加到您的密集排名评估中,并随着年份的变化自动重置排名。
请点击此处了解详情:https://docs.microsoft.com/en-us/sql/t-sql/functions/rank-transact-sql
SELECT
PRHYear AS 'Year',
C.CategoryName AS 'Category',
M.ManufacturerName AS 'Manufacturer',
P.ProductName AS 'Product',
PRHAverageRating AS 'Average Rating',
DENSE_RANK() OVER (Partition by PRHYear ORDER BY PRHAverageRating DESC) AS 'Rank'
FROM
ProductRankingHistory PRH
RIGHT JOIN
Category C ON PRH.PRHCategory = C.CategoryID
RIGHT JOIN
Manufacturer M ON PRH.PRHManufacturer = M.ManufacturerID
LEFT JOIN
Product P ON PRH.PRHProduct = P.ProductID
ORDER BY
PRH.PRHYear ASC, PRH.PRHAverageRating DESC