计算MySQL中的运行总和,当一个字段= 1时递增,而当同一字段= 0时递减1

时间:2018-07-24 01:48:24

标签: mysql

你好,我是MySQL的新手,创建以下MySQL查询时遇到问题。我有一张看起来像这样的桌子:

id   TestCase   PassFail
1     tc_a          1
2     tc_b          0
3     tc_c          1
4     tc_a          0
5     tc_c          1
6     tc_b          1
7     tc_a          1
8     tc_a          1

我想编写一个查询,该查询创建一个表,该表是PassFail字段的运行总和。每当唯一的测试用例首次通过或在失败与通过之间切换时,此字段应增加1;每当测试用例在通过与失败之间切换时,此字段应减少1。当测试用例的新状态保持不变时,总和不应更改。我的示例的输出表应如下所示:

id    TestCase PassFail passSum    note (not in output table)
1      tc_a       1         1      first pass for tc_a, increment by 1 
2      tc_b       0         1      fail for tc_b, no change 
3      tc_c       1         2      first pass for tc_c, increment by 1
4      tc_a       0         1      toggled pass to fail tc_a, decrement by 1
5      tc_c       1         1      no change for tc_c, no change
6      tc_b       1         2      toggled fail to pass tc_b, increment by 1
7      tc_a       1         3      toggled fail to pass tc_a, increment by 1
8      tc_a       1         3      no change for tc_a, no change

我能够使用DISTINCT创建一个运行总计,每当一个新的测试用例通过时,它就会增加,但是我不知道如何在唯一的测试用例失败时使它递减。任何帮助或建议,将不胜感激!

1 个答案:

答案 0 :(得分:0)

您的编辑有很大帮助。

drop table if exists t;
create table t(id int,  TestCase varchar(10),   PassFail int);
insert into t values
(1  ,   'tc_a' ,         1),
(2  ,   'tc_b' ,         0),
(3  ,   'tc_c' ,         1),
(4  ,   'tc_a' ,         0),
(5  ,   'tc_c' ,         1),
(6  ,   'tc_b' ,         1),
(7  ,   'tc_a' ,         1),
(8  ,   'tc_a' ,         1);

在下面的子查询中,我使用带有limit子句的子查询来计算每个测试用例的组合状态。然后,外部查询将使用一个变量根据组合状态计算密码。我认为只有01和10值得关注,我还没有进行广泛的测试,但是结果确实符合您期望的输出。

select s.id,s.testcase,s.passfail,s.combined,
         case 
                when combined = 01 then @passsum:=@passsum + 1
                when combined = 10 then @passsum:=@passsum - 1
                else @passsum:=@passsum
         end passsum
from
(
select t.ID,t.testcase,t.passfail,
         concat (
             case
             when (select passfail from t t1 where t1.testcase = t.testcase and t1.id < t. id order by t1.ID limit 1) is null then 0
           else (select passfail from t t1 where t1.testcase = t.testcase and t1.id < t. id order by t1.ID desc limit 1)
         end,
         t.passfail
         ) as combined
from t
order by t.testcase,t.id
) s
cross join (select @passsum:=0) a
order by s.id;

 +------+----------+----------+----------+---------+
| ID   | testcase | passfail | combined | passsum |
+------+----------+----------+----------+---------+
|    1 | tc_a     |        1 | 01       | 1       |
|    2 | tc_b     |        0 | 00       | 1       |
|    3 | tc_c     |        1 | 01       | 2       |
|    4 | tc_a     |        0 | 10       | 1       |
|    5 | tc_c     |        1 | 11       | 1       |
|    6 | tc_b     |        1 | 01       | 2       |
|    7 | tc_a     |        1 | 01       | 3       |
|    8 | tc_a     |        1 | 11       | 3       |
+------+----------+----------+----------+---------+