如何在不重复条件值的情况下优化查询?

时间:2014-04-04 16:40:13

标签: mysql sql

我尝试使用cia_ensure_values进行优化而不会为每个cia_ensure重复相同的条件

我的桌子:

|policies|
  |id|  |client|  |policy_business_unit_id|  |cia_ensure_id|   |state|       
   1      MATT                  1                   1             0
   2      STEVE                 1                   1             0
   3      BILL                  1                   2             0
   4      LARRY                 1                   2             1
   5      MATT                  1                   2             1
   6      STEVE                 2                   2             2
   7      BILL                  2                   2             2
   8      LARRY                 2                   2             1
   9      MATT                  2                   1             1

|policy_business_units|
   |id|   |name|  |comercial_area_id|
     1     LIFE         1 
     2     ROB          1 
     3     CAR          1 

|comercial_areas|
   |id|   |name|
    1      MICROSOFT
    2      APPLE

|cia_ensures|
   |id|   |name|
    1      ORANGE
    2      BT
    3      ATT
    4      MOVISTAR
    5      SPRINT

以下是信息:

http://sqlfiddle.com/#!2/89491/1

我尝试创建一个查询,当它有值时会在我的查询中重复

SELECT pb.name as unit,
  SUM(CASE WHEN p.state =0 AND ce.id=1 THEN 1 ELSE 0 END ) AS v1, 
  SUM(CASE WHEN p.state =1 AND ce.id=1 THEN 1 ELSE 0 END ) AS a1, 
  SUM(CASE WHEN p.state =2 AND ce.id=1 THEN 1 ELSE 0 END ) AS n1,
  SUM(CASE WHEN p.state =0 AND ce.id=2 THEN 1 ELSE 0 END ) AS v2, 
  SUM(CASE WHEN p.state =1 AND ce.id=2 THEN 1 ELSE 0 END ) AS a2, 
  SUM(CASE WHEN p.state =2 AND ce.id=2 THEN 1 ELSE 0 END ) AS n2,
  SUM(CASE WHEN p.state =0 AND ce.id=3 THEN 1 ELSE 0 END ) AS v3, 
  SUM(CASE WHEN p.state =1 AND ce.id=3 THEN 1 ELSE 0 END ) AS a3, 
  SUM(CASE WHEN p.state =2 AND ce.id=3 THEN 1 ELSE 0 END ) AS n3,
  SUM(CASE WHEN p.state =0 AND ce.id=4 THEN 1 ELSE 0 END ) AS v4, 
  SUM(CASE WHEN p.state =1 AND ce.id=4 THEN 1 ELSE 0 END ) AS a4, 
  SUM(CASE WHEN p.state =2 AND ce.id=4 THEN 1 ELSE 0 END ) AS n4,
  SUM(CASE WHEN p.state =0 AND ce.id=5 THEN 1 ELSE 0 END ) AS v5, 
  SUM(CASE WHEN p.state =1 AND ce.id=5 THEN 1 ELSE 0 END ) AS a5, 
  SUM(CASE WHEN p.state =2 AND ce.id=5 THEN 1 ELSE 0 END ) AS n5 
  FROM cia_ensures ce 
  LEFT JOIN policies p on ce.id = p.cia_ensure_id
  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
  AND ca.id=1  
  Group by p.policy_business_unit_id; 

查询正常运行没有问题并显示此结果:

 UNIT  V1  A1  N1   V2 A2  N2   V3  A3  N3   V4  A4  N4   V5  A5  N5
 LIFE  2   0   0    1   2  0    0   0   0     0   0   0   0    0   0
 ROB   0   1   0    0   1  2    0   0   0     0   0   0   0    0   0 

但是,如果我有100个CIA_ENSURE,会发生什么?我的查询太长了100行。 我怎样才能创建一个值@value,我输入我的cia_ensure,这将重复如下:

  @cia_ensure_values = 1,3,5

  for ce.id=1,3,5

  /////This code will be repeated for all cias typed 
  SELECT pb.name as unit,
  SUM(CASE WHEN p.state =0 AND ce.id=@cia_ensure_values THEN 1 ELSE 0 END ) AS v, 
  SUM(CASE WHEN p.state =1 AND ce.id=@cia_ensure_values THEN 1 ELSE 0 END ) AS a, 
  SUM(CASE WHEN p.state =2 AND ce.id=@cia_ensure_values THEN 1 ELSE 0 END ) AS n,

  end              

  FROM cia_ensures ce 
  LEFT JOIN policies p on ce.id = p.cia_ensure_id
  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
  AND ca.id=1  
  Group by p.policy_business_unit_id; 

有人可以帮我优化这个查询吗?

将接受所有类型的帮助。

请接受各种帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用简短的预备声明轻松完成此操作。只需在where id in()子句中包含您想要的ID:

select distinct @sql := concat('SELECT pb.name as unit,',group_concat(concat(
 'SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,', 
  SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,', 
  SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),
' FROM cia_ensures ce 
  LEFT JOIN policies p on ce.id = p.cia_ensure_id
  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
  AND ca.id=1  
  Group by p.policy_business_unit_id;')
from cia_ensures
where id in(1,3,5);

prepare stmt from @sql;
execute stmt;

SQLFIDDLE:http://sqlfiddle.com/#!2/631b56/23