限制mysql中特定列中的重复行

时间:2010-06-09 17:48:57

标签: mysql

我有这样的查询:

select testset,
count(distinct results.TestCase) as runs,
Sum(case when Verdict = "PASS" then 1 else 0 end) as pass,
Sum(case when Verdict <> "PASS" then 1 else 0 end) as fails,
Sum(case when latest_issue <> "NULL" then 1 else 0 end) as issues,
Sum(case when latest_issue <> "NULL" and issue_type = "TC" then 1 else 0 end) as TC_issues
from results 
 join testcases on results.TestCase = testcases.TestCase
where platform = "T1_PLATFORM" AND testcases.CaseType = "M2"
and testcases.dummy <> "flag_1"
group by testset 
order by results.TestCase

我得到的结果集是:

 testset runs pass fails issues TC_issues
T1  66  125  73  38  33
T2  18  19  16  16  15
T3  57  58  55  55  29
T4  52  43  12  0  0
T5  193  223  265  130  22
T6  23  12  11  0  0

我的问题是,这是一个result表,它有多次运行的测试用例。所以, 我可以使用runs来限制distinct TestCases,但是当我想要passfails,时,因为我使用case我无法消除重复项。

有没有办法实现我想要的?

有什么帮助吗?

感谢。

更新:

TestCases Table : 

ID TestCase CaseType dummy
1  101      M1       flag_0
2  102      M2       flag_1


Results table :

ID TestCase TestSet Verdict latest_issue Platform 
1  101      T1      PASS    NONE         T1_PLATFORM
2  102      T2      FAIL    YES          T2_PLATFORM
3  101      T1      FAIL    YES          T1_PLATFORM

1 个答案:

答案 0 :(得分:0)

我相信你是在追求这样的事情:

select
  testset,
  count(*) as runs,
  sum(passed) as pass,
  count(*) - sum(passed) as fails,
  sum(issues) as issues,
  sum(tc_issues) as tc_issues
from
  ( select 
      r.testset, 
      r.testcase, 
      max(if(r.verdict = 'PASS', 1, 0)) as passed
      max(if(r.latest_issue <> 'NULL', 1, 0)) as issues,
      max(if(r.latest_issue <> 'NULL' and issue_type = 'TC', 1, 0)) as tc_issues,
    from results r
      inner join testcases t on (r.testcase = t.testcase)
    where 
      r.platform = 'T1_PLATFORM' 
      and t.casetype = 'M2'
      and t.dummy <> 'flag_1'
    group by 
      r.testset, 
      r.testcase
  ) as t
group by
  testset
order by 1

或者,如果您愿意,可以使用count而不是sum的版本(应该更有效率):

select
  testset,
  count(*) as runs,
  count(passed) as pass,
  count(*) - count(passed) as fails,
  count(issues) as issues,
  count(tc_issues) as tc_issues
from
  ( select 
      r.testset, 
      r.testcase, 
      max(if(r.verdict = 'PASS', 1, NULL)) as passed
      max(if(r.latest_issue <> 'NULL', 1, NULL)) as issues,
      max(if(r.latest_issue <> 'NULL' and issue_type = 'TC', 1, NULL)) as tc_issues,
    from results r
      inner join testcases t on (r.testcase = t.testcase)
    where 
      r.platform = 'T1_PLATFORM' 
      and t.casetype = 'M2'
      and t.dummy <> 'flag_1'
    group by 
      r.testset, 
      r.testcase
  ) as t
group by
  testset
order by 1

注意:我使用了mysql if函数,因为它比case语法更简洁。如果您更喜欢它,它应该仍然有用。