从唯一列值中获取计数的百分比

时间:2016-04-20 13:08:39

标签: postgresql postgresql-9.1 postgresql-9.3

您好我有一张表格如下

 k_id |l_id |count | avg
----------------------------------
|  1  | 10  |  4   |  
|  1  | 10  |  3   | 
|  1  | 10  |  2   | 
|  1  | 10  |  1   | 
|  1  | 10  |  1   |
|  1  |  9  |  1   |
|  3  |  8  |  1   |
|  2  |  7  |  1   |

在select语句中,我试图从计数中获取百分比,但我不想包含所有行。我想单独计算k_id和l_id是唯一的。

现在我有一个select sub_query,如下所示:

SELECT k_id, l_id, count, 100.0 * count / sum(count) over () AS avg
FROM (                
      select k_id, l_id, count(m_id) as count
         from schema.table                                
               group by k_id, l_id order by count desc
           ) a
     group by k_id, l_id, count

实际结果:

 k_id |l_id |count | avg
-------------------------
|  1  | 10  |  4   | 28.57 
|  1  | 10  |  3   | 21.42
|  1  | 10  |  2   | 14.28
|  1  | 10  |  1   | 7.14
|  1  | 10  |  1   | 7.14
|  1  |  9  |  1   | 7.14
|  3  |  8  |  1   | 7.14
|  2  |  7  |  1   | 7.14

如何获得以下结果

预期结果:

k_id |l_id |count | avg
-------------------------
|  1  | 10  |  4   | 36.36 
|  1  | 10  |  3   | 27.27
|  1  | 10  |  2   | 18.18
|  1  | 10  |  1   | 9.09
|  1  | 10  |  1   | 9.09
|  1  |  9  |  1   | 100.00
|  3  |  8  |  1   | 100.00
|  2  |  7  |  1   | 100.00

感谢。

1 个答案:

答案 0 :(得分:2)

您需要PARTITION SUM超过k_id, l_id。这样您就可以使用k_id, l_id = 1, 10代替整个sum(count)来获得平均值。

SELECT
  k_id, l_id, count,
  100.0 * count / sum(count) OVER (PARTITION BY k_id, l_id) AS avg
FROM ( ... )
ORDER BY l_id DESC;

 k_id | l_id | count |         avg
------+------+-------+----------------------
    1 |   10 |     4 |  36.3636363636363636
    1 |   10 |     3 |  27.2727272727272727
    1 |   10 |     2 |  18.1818181818181818
    1 |   10 |     1 |   9.0909090909090909
    1 |   10 |     1 |   9.0909090909090909
    1 |    9 |     1 | 100.0000000000000000
    3 |    8 |     1 | 100.0000000000000000
    2 |    7 |     1 | 100.0000000000000000