如何根据条件计算列?

时间:2014-04-02 15:06:01

标签: mysql sql

我试图在state = 0时计数,当state = 1时,如果另一个状态没有值,则显示0。

我的桌子:

|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

|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/54d2f/15

如果它不存在或显示为0,我试图获得states = 0,1,2 anc count

Select p.id, p.client, 
  pb.name as BUSINESS_UNITS,
  ce.name as CIA,ca.name as COMERCIAL_AREAS,
  count(p.state) as status_0,count(p.state) as status_1,count(p.state) as status_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

我得到了这个结果:

ID  CLIENT  BUSINESS_UNITS      CIA      COMERCIAL_AREAS    STATE_0 STATE_1   STATUS_2
 1  MATT    LIFE             SPRINT       HOSPITAL                4    4           4

我试图计算如果state = 0,1,2否则在没有值的状态下显示0

如何获得此结果?

ID      CLIENT      BUSINESS_UNITS      CIA        COMERCIAL_AREAS  STATE_0   STATE_1    STATE_2
 1      MATT         LIFE              SPRINT       HOSPITAL          1         0         0
 2      STEVE        ROB               SPRINT       BANK              1         0         0
 3      BILL         SECURE            APPLE        HOSPITAL          0         1         0
 4      LARRY        ACCIDENT          APPLE        BANK              0         1         0

请相信我会得到各种帮助。 感谢。

3 个答案:

答案 0 :(得分:2)

这应该可以解决问题。做一个if条件。如果state = 0则count另外输入0 见工作FIDDLE

SELECT
    p.id,
    p.client,
    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
GROUP BY pb.id
ORDER BY p.id;

如果您想要考虑空状态,这将是查询.. FIDDLE

SELECT
    p.id,
    p.client,
    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 IS NULL, count(p.id), 0) AS state_null
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
GROUP BY pb.id
ORDER BY p.id;

答案 1 :(得分:1)

SELECT SUM( CASE WHEN [condition] THEN 1
                                  ELSE 0 END) AS count
FROM [your tables] 

答案 2 :(得分:1)

试试这个:

SELECT
    p.id,
    p.client,
    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 OR p.state IS NULL, count(ISNULL(p.state)), 0) AS state_2_and_null
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
GROUP BY pb.name
ORDER BY p.id;

如果您的状态多于[ nil,0,1,2 ],并且您希望它们全部位于最后一列state_2_and_null下,则可以更改条件{{1转到p.state = 2