Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:46:21+00:00 2026-06-18T06:46:21+00:00

I have created a simple database to store test results, but I am struggling

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:46:22+00:00Added an answer on June 18, 2026 at 6:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a simple website but I need to store and extract files
I have created a simple windows service that periodically checks a remote database via
I have created a simple test form with FormBorderStyle = FixedToolWindow by default and
I have created a simple facebook app and I want to store the facebook
I have to store XML content in a SQL Server 2008R2 database. The XML
I have created a simple products table with sqlite database using ruby. I can
Could someone help me on this, I have created simple web services using axis2
How to manually create Friendly URLs? (PHP) So I have created simple php file
I am new to Repository concept and get some questions. I have created simple
I have created the simple web service. Code: [ServiceContract] public interface ITsdxService { [OperationContract]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.