Mysql 如何对组值求和?

Mysql 如何对组值求和?,mysql,sql,Mysql,Sql,当组状态=0,1,2时,我是sum,当它显示为0时,我是sum 我的桌子: |policies| |id| |client| |policy_business_unit_id| |cia_ensure_id| |state| 1 MATT 1 1 0 2 STEVE 2 1

当组状态=0,1,2时,我是sum,当它显示为0时,我是sum

我的桌子:

|policies|
  |id|  |client|  |policy_business_unit_id|  |cia_ensure_id|   |state|       
   1      MATT                  1                   1             0
   2      STEVE                 2                   1             0
   3      BILL                  3                   2             1
   4      LARRY                 4                   2             1
   5      MESSI                 1                   1             1  
   6      DROGBA                1                   1             1   

|policy_business_units|
   |id|   |name|  |comercial_area_id|
     1     LIFE         2 
     2     ROB          1
     3     SECURE       2
     4     ACCIDENT     1

|comercial_areas|
   |id|   |name|
    1      BANK
    2      HOSPITAL

|cia_ensures|
   |id|   |name|
    1      SPRINT
    2      APPLE
资料如下:

http://sqlfiddle.com/#!2/935e78/4
我试图对每列中的状态求和=0,1,2:

  SELECT 
   IF( p.state =0, COUNT( p.state ) , 0 ) AS state_0, 
   IF( p.state =1, COUNT( p.state ) , 0 ) AS state_1,
   IF( p.state =2, COUNT( p.state ) , 0 ) AS state_2, 
   p.policy_business_unit_id AS UNITS, 
   p.cia_ensure_id AS CIAS

FROM policies p
WHERE  p.policy_business_unit_id IN ( 1 ) 
AND p.cia_ensure_id =1
GROUP BY p.state
结果是:

  STATE_0   STATE_1       STATE_2     UNITS    CIAS
    1           0          0           1       1
    0           2          0           1       1
我怎样才能得到这个结果

  STATE_0   STATE_1       STATE_2     UNITS    CIAS
    1           2          0           1       1
请允许我感谢您的帮助。 谢谢。

更换这个

  SELECT 
   IF( p.state =0, COUNT( p.state ) , 0 ) AS state_0, 
   IF( p.state =1, COUNT( p.state ) , 0 ) AS state_1,
   IF( p.state =2, COUNT( p.state ) , 0 ) AS state_2, 


如果您需要将state=0、1或2的所有值组合在一起,那么您的查询必须稍微不同。试试这个

SELECT 
    SUM(CASE WHEN p.state = 0 THEN 1 ELSE 0 END) AS state_0,
    SUM(CASE WHEN p.state = 1 THEN 1 ELSE 0 END) AS state_1,
    SUM(CASE WHEN p.state = 2 THEN 1 ELSE 0 END) AS state_2,
    p.policy_business_unit_id AS UNITS, 
    p.cia_ensure_id AS CIAS 
FROM policies p
WHERE  p.policy_business_unit_id IN ( 1 ) 
    AND p.cia_ensure_id =1
GROUP BY CASE WHEN p.state IN (0,1,2) THEN 1 ELSE 0 END

这里有一个工作说明了这是如何工作的。

您可以按照策略(业务)部门(id)和中央情报局(cia)部门(id)进行分组,部分原因是您可能希望报告的不仅仅是1和1的组合,还包括所有组合

SELECT 
sum(case when p.state = 0 then 1 else 0 end) AS state_0, 
sum(case when p.state = 1 then 1 else 0 end) AS state_1,
sum(case when p.state = 2 then 1 else 0 end) AS state_2, 
p.policy_business_unit_id AS UNITS, 
p.cia_ensure_id AS CIAS
FROM policies p
--WHERE  p.policy_business_unit_id IN ( 1 ) 
--AND p.cia_ensure_id =1
group by p.policy_business_unit_id, p.cia_ensure_id 

您编写的查询按
状态
分组,记录中有2个状态,因此有2个记录。试试看,这会给你你所需要的。只需删除
按p.state分组
,你就可以了。我尝试了你的代码,但我有两行,应该只有一行
SELECT 
sum(case when p.state = 0 then 1 else 0 end) AS state_0, 
sum(case when p.state = 1 then 1 else 0 end) AS state_1,
sum(case when p.state = 2 then 1 else 0 end) AS state_2, 
p.policy_business_unit_id AS UNITS, 
p.cia_ensure_id AS CIAS
FROM policies p
--WHERE  p.policy_business_unit_id IN ( 1 ) 
--AND p.cia_ensure_id =1
group by p.policy_business_unit_id, p.cia_ensure_id