Mysql 如何重复值、计数和分组?

Mysql 如何重复值、计数和分组?,mysql,sql,Mysql,Sql,我试图创建一个交叉表,但我需要一种查询,重复cia确保的每个策略、业务和单位组 我的桌子: |policies| |id| |client| |policy_business_unit_id| |cia_ensure_id| |state| 1 MATT 1 1 0 2 STEVE 2

我试图创建一个交叉表,但我需要一种查询,重复cia确保的每个策略、业务和单位组

我的桌子:

|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             0
   4      LARRY                 4                   2             1

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

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

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

我正在尝试获取states=0,1,2 anc计数,如果它不存在或显示为0

Select  
  pb.name as BUSINESS_UNITS,
  ce.name as CIA,ca.name as COMERCIAL_AREAS,
  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
From policies p 
  INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id
  INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id
  INNER JOIN cia_ensures ce ON ce.id = p.cia_ensure_id
where ca.id=1
group by pb.id
我得到了这个结果:

BUSINESS_UNITS      CIA      COMERCIAL_AREAS    STATE_0   STATE_1   STATUS_2
 LIFE            SPRINT       BANK                  1       0           0
 ROB             SPRINT       BANK                  1       0           0
 SECURE          APPLE        BANK                  1       0           0
 ACCIDENT        APPLE        BANK                  0       1           0
我的问题是:

我怎样才能得到这个结果

BUSINESS_UNITS      CIA      COMERCIAL_AREAS    STATE_0   STATE_1   STATUS_2
 LIFE            SPRINT       BANK                  1       0           0
 ROB             SPRINT       BANK                  1       0           0
 SECURE          SPRINT       BANK                  0       0           0
 ACCIDENT        SPRINT       BANK                  0       0           0
 LIFE            APPLE        BANK                  0       0           0
 ROB             APPLE        BANK                  0       0           0
 SECURE          APPLE        BANK                  1       0           0
 ACCIDENT        APPLE        BANK                  0       1           0
我试过了

SELECT 
   COALESCE(SUM(CASE WHEN p.state = 0 THEN 1 ELSE 0 END),0) AS state_0,
   COALESCE(SUM(CASE WHEN p.state = 1 THEN 1 ELSE 0 END),0) AS state_1,
   COALESCE(SUM(CASE WHEN p.state = 2 THEN 1 ELSE 0 END),0) AS state_2,
   count(*) AS total, p.policy_business_unit_id as units,
   p.cia_ensure_id AS cias, p.state as status
   FROM policies p
   WHERE policy_business_unit_id IN (1)
   AND cia_ensure_id IN (1,2)
   GROUP BY cia_ensure_id
有人知道怎么做

请允许我感谢您的帮助。
谢谢。

您必须使用子查询,然后应用
左外部联接
将它们联接在一起。顶部的
IFNULL
部分确保由外部联接产生的不存在状态字段中填充的
NULL
值转换为0

SET @id = 1;

SELECT sections.BUSINESS_UNITS, sections.CIA, sections.AREAS,
    IFNULL(states.state_0, 0) AS STATE_0,
    IFNULL(states.state_1, 0) AS STATE_1,
    IFNULL(states.state_2, 0) AS STATE_2
    FROM (
        SELECT
            pb.id AS bus_id, pb.name AS BUSINESS_UNITS,
            ce.id AS cia_id, ce.name AS CIA,
            ca.name AS AREAS
            FROM policies p 
            INNER JOIN policy_business_units pb
            ON pb.id = p.policy_business_unit_id
            INNER JOIN comercial_areas ca
            ON ca.id = pb.comercial_area_id
            INNER JOIN cia_ensures ce
            WHERE ca.id = @id
    ) AS sections
    LEFT OUTER JOIN (
        SELECT
            pb.id AS bus_id,
            ce.id AS cia_id,
            if (p.state = 0, 1, 0) AS state_0,
            if (p.state = 1, 1, 0) AS state_1,
            if (p.state = 2, 1, 0) AS state_2
            FROM policies p 
            INNER JOIN policy_business_units pb
            ON pb.id = p.policy_business_unit_id
            INNER JOIN comercial_areas ca
            ON ca.id = pb.comercial_area_id
            INNER JOIN cia_ensures ce
            ON ce.id = p.cia_ensure_id
            WHERE ca.id = @id
    ) AS states
    ON sections.bus_id = states.bus_id
    AND sections.cia_id = states.cia_id
    ORDER BY sections.cia_id;

DEMO@

@CarlitosMorales-正如您所看到的,sql fiddle可以工作,因此它不是查询。你的桌子模式和你提供的小提琴完全一样吗?您必须用所需的值替换3个
@id
变量。当崩溃发生时,确切的错误消息是什么?@CarlitosMorales-对我来说,它在mysql客户端和phpmyadmin中本地运行时,大约0.0077秒就没有问题了。同时,它也在起作用。关于sql FIDLE,您的表模式是否有任何不同?如果没有,您能否为这些表提供更多的示例数据?表中有多少行用于测试?您是否使用与小提琴中完全相同的设置和数据进行了测试?请先检查这是否有效。@CarlitosMorales-请使用完全相同的数据和模式!是的,我很高兴,但从这一点可以看出,查询中的总和在哪里?是的,它工作并更改了策略_business_unit=1,因为我有很多信息,现在我正在尝试获取总和=),但感谢您的帮助,这将有所帮助。非常感谢师父的帮助。