在mysql中求和(IF)

时间:2013-06-16 04:20:32

标签: mysql count sum

我可以在mysql中获得多个条件的错误计数总和吗?

我的要求是,我需要获得具有一定优先权的bug的总和或数量,比如P1,超过特定时间,但超出的数量应该从该特定优先级的总数中计算。

我的查询是: -

select 
sum(
 IF(priority="P1",1,0)) P1, 
sum(
 IF(timediff(delta_ts,creation_ts) > "00:00:05",1,0))P1_exeeded,
SUM(
 IF(priority="P2",1,0)) P2,
sum(
 IF(timediff(delta_ts,creation_ts) > "00:00:10",1,0))P2_exeeded,
SUM(
 IF(priority="P3",1,0)) P3count,
SUM(
 IF(priority="P4",1,0)) P4count,
SUM(
 IF(priority="P5",1,0)) P5count,
SUM(
 IF(priority="P6",1,0)) P6count,
SUM(
 IF(priority="P7",1,0)) P7count,
SUM(
 IF(priority="P8",1,0)) P8count 
from bugs 
where bugs.product_id=237 
and bugs.resolution='FIXED' 
and bugs.creation_ts >='2013-06-14 09:00:00' 
and bugs.creation_ts <= '2013-06-16 08:59:59' 
and bug_status="RESOLVED";

我的结果如下: -

------+------------+------+------------+---------+---------+---------+---------+---------+---------+
| P1   | P1_exeeded | P2   | P2_exeeded | P3count | P4count | P5count | P6count | P7count | P8count |
+------+------------+------+------------+---------+---------+---------+---------+---------+---------+
|    7 |         19 |    6 |         18 |       5 |       1 |       0 |       0 |       0 |       0 |
+------+------------+------+------------+---------+---------+---------+---------+---------+---------+
1 row in set (0.00 sec)

但是我需要从每个优先级中获​​得超出的bug id的总和,通过以下两个条件: -

超过特定时间限制的超出错误数的总和,并从特定优先级类别的计数中获取。

错误ID的超出总和应该满足每种情况下的两种情况。

P1优先级错误ids我有。 P1_exeeded = IF(timediff(delta_ts,creation_ts)&gt;“00:00:05”,1,0))取自P1 bugids的数量。

请帮助在mysql中使用sum(满足2个条件)

2 个答案:

答案 0 :(得分:2)

我得到了答案。谢谢Chetan。: -

select 
sum(
 IF(priority="P1",1,0)) P1,
sum( 
 IF((timediff(delta_ts,creation_ts) > "00:02:00") && (priority="P1") ,1,0))P1_exeeded,
SUM(
 IF(priority="P2",1,0)) P2,sum( IF((timediff(delta_ts,creation_ts) > "00:01:00") && (priority="P2") ,1,0))P2_exeeded,
SUM(
 IF(priority="P3",1,0)) P3count,
SUM(
 IF(priority="P4",1,0)) P4count,
SUM(
 IF(priority="P5",1,0)) P5count,
SUM(
 IF(priority="P6",1,0)) P6count,
SUM(
 IF(priority="P7",1,0)) P7count,
SUM(
  IF(priority="P8",1,0)) P8count 
from bugs ....
where
.....
+------+------------+------+------------+---------+---------+---------+---------+---------+---------+
| P1   | P1_exeeded | P2   | P2_exeeded | P3count | P4count | P5count | P6count | P7count | P8count |
+------+------------+------+------------+---------+---------+---------+---------+---------+---------+
|    7 |          1 |    6 |          1 |       5 |       1 |       0 |       0 |       0 |       0 |
+------+------------+------+------------+---------+---------+---------+---------+---------+---------+

答案 1 :(得分:0)

SELECT `customer xxx`,
cxType,
`Order Store`,
sum(if(category='xxxx',qty,0)), 
min(`order date`) as 'First Purchcase Date'
FROM table1
group by `customer xxx`;