1
我想顯示百分比評級產品評論表按訂單號排序。添加行到最終選擇,如果爲空查詢sql
例如:
:71%
:0%
:0%
:28%
:0%
可以是任何數量的速度不會在產品的評價表中存在(在上面的例子中速率4,3,1在我的表不存在)
DATAS在示例上方插入在我的表。是:
Id CustomerId ProductId Rating
------- --------------- -------------- --------
39 14 57 2
42 18 57 5
56 19 57 5
我作秀%的查詢是:
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
,並導致我的查詢是:
ProductId Rating percentage
----------- ----------- -----------
57 5 71
57 2 28
但我不怎麼顯示其他費率數字,如果不存在爲零百分比。
感謝所有。
您正在過濾productid = 57.所以,您將只有產品ID 57 – 2017-09-23 10:52:53