简体   繁体   中英

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.

The idea is that a test run (TRID) will have severaltest sets (TSID), each with several test cases (TCID).

  1. Total should equal amount of test cases for that TSID
  2. Pass should equal amount of test cases with all steps of StatusID=1
  3. Fail should equal amount of test cases with 1 or more step of StatusID=2.
  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. If there is a failed step, then test case should fail as per above.

SQL to create a simplified example of my Results table:-

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:-

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:-

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. The outer query could then calculate the statistics per test set (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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