简体   繁体   English

SQL Server 2008计数

[英]SQL Server 2008 count

I have created a simple database to store test results, but I am struggling with the SQL Count to total up my pass/fail/warning to present this. 我已经创建了一个简单的数据库来存储测试结果,但是我在SQL计数方面苦苦挣扎,以总结出我的通过/失败/警告。

The idea is that a test run (TRID) will have severaltest sets (TSID), each with several test cases (TCID). 这个想法是,一个测试运行(TRID)将具有多个测试集(TSID),每个测试集都有多个测试用例(TCID)。

  1. Total should equal amount of test cases for that TSID 总数应等于该TSID的测试用例数量
  2. Pass should equal amount of test cases with all steps of StatusID=1 通过应与StatusID = 1的所有步骤相等的测试用例数量
  3. Fail should equal amount of test cases with 1 or more step of StatusID=2. 失败的测试用例数量应等于StatusID = 2的1个或更多步骤。
  4. Warning should equal amount of test cases with 1 or more step of StatusID=3, but the same test cases should have zero fails in it. 警告应与StatusID = 3等于或大于1步的测试用例相等,但相同的测试用例应具有零个失败。 If there is a failed step, then test case should fail as per above. 如果步骤失败,则测试用例应如上所述失败。

SQL to create a simplified example of my Results table:- 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

My current SQL (which you will see provides me with the wrong warning count:- 我当前的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

From the above SQL, the current incorrect results are:- 从上面的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*

Results should be.... 结果应该是...

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*  

Thanks 谢谢

You could calculate the statistics per test case (TCID) in a subquery. 您可以在子查询中计算每个测试用例的统计信息(TCID)。 The outer query could then calculate the statistics per test set (TSID). 然后,外部查询可以计算每个测试集(TSID)的统计信息。 For example: 例如:

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

Live example at SQL Fiddle. SQL Fiddle的实时示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM