有没有一种方法可以对SQL中的组执行操作?

时间:2019-04-17 05:35:31

标签: sql sqlite aggregate-functions

所以我对SQL还是很陌生,可能没有准确描述我想做的事情。我有一个包含三列的表,我想按一个列分组,然后在另一列中查看每个组的百分比是多少。例如在表中:

id   col1  col2
----------------
0       A     1
1       A     2
2       B     2
3       B     2
4       A     1

我想按col1分组,并查看每个组(AB)中col2中值为1的百分比。我想要的结果是:

col1  percentage_col2_equals_1
------------------------------
   A                      66.7
   B                       0.0

到目前为止,我有:

SELECT col1,
((SELECT COUNT(*) FROM my_table
WHERE col2 = 1
GROUP BY col1) / 
(SELECT COUNT(*) FROM my_table
GROUP BY col1) * 100)
FROM my_table
GROUP BY col1;

但这不起作用。任何帮助将不胜感激!

6 个答案:

答案 0 :(得分:1)

使用SELECT col1,(coalesce(count(case when col2=1 then col2 end),0)*100.00)/count(*) from tablename group by col1

COUNT()

答案 1 :(得分:1)

与每个人相同的答案,只是出于Postgres的表现力而把它放在这里:)

实时测试:https://www.db-fiddle.com/f/goL488VaPuZYii7Wik3pFk/4

select
    col1,
    count(*) filter(where col2 = 1) ::numeric / count(*)

from tbl    
group by col1;

输出:

| col1 | ?column?               |
| ---- | ---------------------- |
| A    | 0.66666666666666666667 |
| B    | 0.00000000000000000000 |

要以百分比形式显示它并保留小数点后一位,请将其乘以100并四舍五入为1:

实时测试:https://www.db-fiddle.com/f/goL488VaPuZYii7Wik3pFk/5

select
    col1,    
    round( 
      count(*) filter(where col2 = 1) ::numeric / count(*) * 100, 
      1 
    ) as p_a      
from tbl    
group by col1;


select
    col1,    
    (
        count(*) filter(where col2 = 1) ::numeric / count(*) * 100
    )::numeric(100,1) as p_b       
from tbl    
group by col1;

输出:

| col1 | p_a  |
| ---- | ---- |
| A    | 66.7 |
| B    | 0.0  |


| col1 | p_b  |
| ---- | ---- |
| A    | 66.7 |
| B    | 0.0  |

答案 2 :(得分:0)

以下查询将返回您的预期结果:

SELECT col1, 
       CAST(((SUM(IIF(col2 = 1, 1, 0))) * 100.0) / COUNT(*) AS DECIMAL(5, 1)) AS percentage_col2_equals_1
FROM my_table
GROUP BY col1;

使用示例数据执行示例:

DECLARE @my_table TABLE (id INT,  col1 CHAR(1),  col2 INT);

INSERT INTO @my_table (id, col1, col2) VALUES
(0, 'A', 1),
(1, 'A', 2),
(2, 'B', 2),
(3, 'B', 2),
(4, 'A', 1);

SELECT col1, CAST(((SUM(IIF(col2 = 1, 1, 0))) * 100.0) / COUNT(*) AS DECIMAL(5, 1)) AS percentage_col2_equals_1
FROM @my_table
GROUP BY col1;

输出:

col1    percentage_col2_equals_1
---------------------------------
A       66.7
B       0.0

答案 3 :(得分:0)

     CREATE TABLE #TEMP
    (ID   INT, 
     COL1 VARCHAR(10), 
     COL2 INT
    );
    INSERT INTO #TEMP
           SELECT 0, 'A',1
           UNION
           SELECT 1, 'A',2
           UNION
           SELECT 2, 'B',2
           UNION
           SELECT 3, 'B',2
           UNION
           SELECT 4, 'A',1;
    SELECT T.COL1, 
           ROUND((CAST(COUNT(CASE
                                 WHEN T.COL2 = 1
                                 THEN T.COL2
                                 ELSE NULL
                             END) AS DECIMAL) / (S.COL2)) * 100.0, 2) AS Percentage_1
    FROM #TEMP T
         JOIN
    (
        SELECT COUNT(COL2) COL2, 
               COL1
        FROM #TEMP
        GROUP BY COL1
    ) S ON S.COL1 = T.COL1
    GROUP BY T.COL1, 
             S.COL2;

答案 4 :(得分:0)

这将起作用:

CREATE TABLE Table1
    ("id" int, "col1" varchar2(1), "col2" int)
;
//do inserts
select aa."col1",((select count(*) from Table1 b 
where b."col1"=aa."col1" and b."col2"=1 )*100/(select count(*) 
from Table1 c where c."col1"='A' )) percentge
from Table1 aa
group by aa."col1"
; 

输出:

A   66.66666666666666666666666666666666666667
B   0

答案 5 :(得分:0)

在SQLite中,表达式col2 = 11时返回true,在0时返回false
因此,您只需要col2 = 1的平均值,然后将其舍入为小数点后一位:

select
  col1,
  round(100.0 * avg(col2 = 1), 1) percentage_col2_equals_1
from tablename  
group by col1

请参见demo
结果:

| col1 | percentage_col2_equals_1 |
| ---- | ------------------------ |
| A    | 66.7                     |
| B    | 0                        |