问题描述
我有一张桌子:
------------------------
testId | runId |result
------------------------
**1 | 11 |0**
**1 | 12 |1**
**1 | 13 |1**
**1 | 14 |1**
**2 | 21 |0**
**3 | 31 |1**
**4 | 41 |1**
**4 | 42 |1**
**4 | 43 |1**
**5 | 51 |0**
**5 | 52 |0**
**5 | 53 |0**
**6 | 61 |1**
**6 | 62 |0**
**6 | 63 |1**
对于测试,可以有多个运行/执行。每次运行都有结果。对于结果列,这里0表示失败,1表示通过。 我想查询 -如果所有运行PASS进行测试,则TotalStatus为PASS -如果所有测试都失败了,则TotalStatus失败 -如果其中一些通过并且其中一些最终完成,则TotalStaus处于缺陷状态
我想从上表中获取输出,例如
testId | numOfRun | OverallStatus
1 | 4 |缺陷
2 | 1 |失败
3 | 1 |通过
4 | 3 |通过
5 | 3 |失败
6 | 3 |缺陷
解决方法
您可以使用条件聚合
select testId,numOfRun,case when numOfRun = pass then 'pass'
when numOfRun = fail then 'fail'
else 'defect'
end as OverallStatus
from (
select testId,count(*) numOfRun,sum(case when result = 0 then 1 else 0 end) as fail,sum(case when result = 1 then 1 else 0 end) as pass
from table
group by testId
) t
,
我建议一步一步完成>
select testid,(case when min(result) = 1 then 'Pass'
when max(result) = 0 then 'Fail'
else 'Defect'
end) as overall_status
from t
group by testid;
编辑:
根据您的评论:
select testid,(case when min(result) = N'TestSuccess' then 'Pass'
when max(result) = N'TestFailure' then 'Fail'
else 'Defect'
end) as overall_status
from t
group by testid;