2013-02-04 136 views
2

我已經創建了一個簡單的數據庫來存儲測試結果,但我正在努力與SQL計數總計我的通過/失敗/警告來提出這一點。SQL Server 2008計數

想法是測試運行(TRID)將有多個測試集(TSID),每個測試集都有幾個測試用例(TCID)。

  1. 總應的測試用例等量該TSID
  2. 通行證應的測試用例等量的StatusID = 1
  3. 故障的所有步驟應的測試用例等量的StatusID的1個或多個步驟= 2。
  4. 警告應該等於具有一個或多個StatusID = 3步驟的測試用例的數量,但相同的測試用例應該有零個失敗。如果有一個失敗的步驟,那麼測試用例應該按照上面的方法失敗。

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* 

感謝

+0

哪些列的名稱? –

回答

4

你可以在子查詢計算每個測試用例(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 

Live example at SQL Fiddle.

+0

謝謝,這正是我正在尋找的! – muckus