i want show percent rating product reviews table order by rate number.
for example:
5: 71%
4: 0%
3: 0%
2: 28%
1: 0%
maybe any rate number Not exist in product reviews table.(in above example rate 4,3,1 not exist in my table)
datas inserted in my table at above example is:
Id CustomerId ProductId Rating
------- --------------- -------------- --------
39 14 57 2
42 18 57 5
56 19 57 5
my query for show percent is:
SELECT
pr.ProductId ,
pr.Rating,
percentage = AVG(pr.Rating) * 100 / SUM(AVG(pr.Rating)) OVER (PARTITION BY pr.ProductId)
FROM ProductReview pr
WHERE pr.ProductId = 57
GROUP BY
pr.ProductId,
pr.Rating
ORDER BY pr.Rating DESC
And result my query is:
ProductId Rating percentage
----------- ----------- -----------
57 5 71
57 2 28
but i don't how to show other rate number if not exist as zero percent.
thanks to all.