我创建了一个简单的数据库来存储测试结果,但我正在努力处理 SQL 计数来汇总我的通过/失败/警告来呈现此结果。
这个想法是,一次测试运行 (TRID) 将有多个测试集 (TSID),每个测试集都有多个测试用例 (TCID)。
- 该 TSID 的测试用例总数应相等
- 通过的测试用例数量应与 StatusID=1 的所有步骤相同
- 失败的测试用例数量应与 StatusID=2 的 1 个或多个步骤相同。
- 警告应具有 1 个或多个 StatusID=3 步骤的相同数量的测试用例,但相同的测试用例中应有 0 个失败。如果有一个失败的步骤,那么测试用例应该按照上面的方式失败。
使用 SQL 创建结果表的简化示例:-
create table Results (StatusID int, TRID int, TSID int, TCID int);
--Test Set 1 / Test Case 1.
INSERT INTO Results VALUES (1, 1, 1, 1)
INSERT INTO Results VALUES (1, 1, 1, 1)
INSERT INTO Results VALUES (1, 1, 1, 1)
--Test Set 1 / Test Case 2
INSERT INTO Results VALUES (1, 1, 1, 2)
INSERT INTO Results VALUES (1, 1, 1, 2)
--Test Set 2 / Test Case 1
INSERT INTO Results VALUES (1, 1, 2, 1)
INSERT INTO Results VALUES (1, 1, 2, 1)
INSERT INTO Results VALUES (1, 1, 2, 1)
--Test Set 2 / Test Case 2
INSERT INTO Results VALUES (1, 1, 2, 2)
INSERT INTO Results VALUES (2, 1, 2, 2)
--Test Set 3 / Test Case 1
INSERT INTO Results VALUES (1, 1, 3, 1)
INSERT INTO Results VALUES (1, 1, 3, 1)
INSERT INTO Results VALUES (1, 1, 3, 1)
--Test Set 3 / Test Case 2
INSERT INTO Results VALUES (1, 1, 3, 2)
INSERT INTO Results VALUES (3, 1, 3, 2)
--Test Set 4 / Test Case 1
INSERT INTO Results VALUES (1, 1, 4, 1)
INSERT INTO Results VALUES (1, 1, 4, 1)
INSERT INTO Results VALUES (1, 1, 4, 1)
--Test Set 4 / Test Case 2
INSERT INTO Results VALUES (3, 1, 4, 2)
INSERT INTO Results VALUES (2, 1, 4, 2)
SELECT * FROM Results
我当前的 SQL(您将看到它为我提供了错误的警告计数:-
DECLARE @trid INT
SET @trid = 1
SELECT TRID, TSID,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE R.TRID = @trID AND R.TSID = TSID) As Total,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID) - (SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID AND StatusID = 2) - (SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID AND StatusID = 3 AND (SELECT COUNT(DISTINCT TCID) FROM Results WHERE TRID = @trID AND R.TSID = TSID AND StatusID = 2) = 0) AS Pass,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE R.TSID = TSID AND StatusID=2) As Fail,
(SELECT COUNT(DISTINCT TCID) FROM Results WHERE R.TSID = TSID AND StatusID=3) As Warning
FROM Results R
WHERE TRID = @TRID
GROUP BY TRID, TSID
从上面的SQL来看,当前错误的结果是:-
TRID TSID Total Pass Fail Warning
1 1 2 2 0 0
1 2 2 1 1 0
1 3 2 1 0 1
1 4 2 1 1 1*
结果应该是......
TRID TSID Total Pass Fail Warning
1 1 2 2 0 0
1 2 2 1 1 0
1 3 2 1 0 1
1 4 2 1 1 0*
谢谢
最佳答案
您可以在子查询中计算每个测试用例 (TCID) 的统计信息。然后,外部查询可以计算每个测试集的统计信息 (TSID)。例如:
select TRID
, TSID
, count(*) as Total
, sum(case when FailSteps = 0 and WarnSteps = 0 then 1 else 0 end) as Pass
, sum(case when FailSteps > 0 then 1 else 0 end) as Fail
, sum(case when FailSteps = 0 and WarnSteps > 0 then 1 else 0 end) as Warning
from (
select TRID
, TSID
, TCID
, sum(case when StatusID = 1 then 1 else 0 end) as PassSteps
, sum(case when StatusID = 2 then 1 else 0 end) as FailSteps
, sum(case when StatusID = 3 then 1 else 0 end) as WarnSteps
from Results
group by
TRID
, TSID
, TCID
) as TestCases
group by
TRID
, TSID
关于SQL Server 2008 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14683556/