计算SQL百分比

时间:2014-12-24 13:41:38

标签: sql sql-server

表格“测试”:

+---------+------+-------+
| Section | Test | Count |
+---------+------+-------+
| A       | T1   |    20 |
| A       | T2   |    10 |
| A       | T3   |    15 |
| A       | T4   |    30 |
| B       | T1   |    10 |
| B       | T2   |    40 |
| B       | T3   |    25 |
| B       | T4   |    30 |
+---------+------+-------+

我想按部分计算每个测试组的计数百分比(例如:第一次计算:(20 * 100)/(20 + 10 + 15 + 30))。结果表应如下所示。

+---------+------+-------+------------+
| Section | Test | Count | Percentage |
+---------+------+-------+------------+
| A       | T1   |    20 | 26.6       |
| A       | T2   |    10 | 13.3       |
| A       | T3   |    15 | 20         |
| A       | T4   |    30 | 40         |
| B       | T1   |    10 | 9.5        |
| B       | T2   |    40 | 38.1       |
| B       | T3   |    25 | 23.8       |
| B       | T4   |    30 | 28.6       |
+---------+------+-------+------------+

请让我知道任何好的解决方案。

0 个答案:

没有答案