我是新來的窗口函數。這是有4種水果的原始桌子。我可以使用PARTITION BY和GROUP BY子句嗎? SQL Server 2012
fruit quantity
orange 100
banana 27
banana 20
orange 5
melon 5
apple 1
banana 10
banana 4
banana 36
banana 86
banana 47
apple 32
banana 7
banana 5
banana 3
是否有可能將這個變成每個水果構成一切總量的百分比?下面是我想:
fruit total percentage
apple 33 9%
banana 245 63%
orange 105 27%
melon 5 1%
這裏是我想出來的代碼,但它給我一個錯誤:
SELECT fruit
, SUM(quantity)/SUM(quantity) OVER() * 100
FROM fruit_inventory
GROUP BY fruit
如果我撤消GROUP BY
並刪除第一個SUM(quantity)
然後我得到看起來像這樣的多個記錄:
fruit quantity percentage
apple 1 0%
apple 32 8%
banana 27 7%
banana 20 5%
banana 10 3%
banana 4 1%
banana 36 9%
banana 86 22%
banana 47 12%
banana 7 2%
banana 5 1%
banana 3 1%
melon 5 1%
orange 100 26%
orange 5 1%
在您的OVER子句中添加ORDER BY水果 – Mihai
仍然失敗。我應該刪除什麼? – eek142
錯誤是什麼,可能會嘗試在添加一些偏執之後與100相乘 – Mihai