[SQL Server] 比率を計算する簡単な方法

inno
2024-01-18 13:34 371 0
[SQL Server] 比率を計算する簡単な方法

SELECT *, ROUND(CONVERT(float, Cnt) * 100.0 / SUM(Cnt) OVER(),1) AS Rate
FROM (
SELECT 'A' AS Cate, 50 AS Cnt UNION ALL
SELECT 'B' AS Cate, 10 AS Cnt UNION ALL
SELECT 'C' AS Cate, 20 AS Cnt UNION ALL
SELECT 'D' AS Cate, 30 AS Cnt
) AS A

結果
Cate Cnt         Rate
---- ----------- ---------------------------------------
A    50          45.4
B    10          9.1
C    20          18.2
D    30          27.3

(4 行処理されました)

コメント