0

大家好,我在 sql server 中进行查询时遇到问题。

我有 3 个表:testcases、executions 和 execution_bugs:

测试用例

id | name
-------------
1  | Login
2  | Logout

处决

id | testcase_id
-----------------
1  | 1
2  | 2
3  | 1

执行错误

execution_id | bug_id
----------------------
1            | B-1
3            | B-2

而且我需要知道定义了多少测试用例,有多少已经执行,有多少测试用例有错误,有多少没有。

我正在寻找一个可以给我这种结果的查询:

testcases_n | executed | with_bugs | without_bugs | bugs_amount
---------------------------------------------------------------
2           | 2        | 1         | 1            | 2

考虑到表结构,这可能吗?

谢谢!

4

1 回答 1

0

你的意思是这样的:

declare @testcases as table ( id int, name varchar(16) )
insert into @testcases ( id, name ) values
  ( 1, 'login' ), ( 2, 'Logout' )

declare @executions as table ( id int, testcase_id int )
insert into @executions ( id, testcase_id ) values
  ( 1, 1 ), ( 2, 2 ), ( 3, 1 )

declare @execution_bugs as table ( execution_id int, bug_id varchar(16) )
insert into @execution_bugs ( execution_id, bug_id ) values
  ( 1, 'B-1' ), ( 3, 'B-2' )

select
  ( select count(42) from @testcases ) as testcases_n,
  ( select count(distinct testcase_id) from @executions ) as executed,
  ( select count(distinct e.testcase_id) from @executions as e inner join @execution_bugs as eb on eb.execution_id = e.id ) as with_bugs,
  ( select count(42) from @testcases ) - ( select count(distinct e.testcase_id) from @executions as e inner join @execution_bugs as eb on eb.execution_id = e.id ) as without_bugs,
  ( select count(42) from @execution_bugs ) as bugs_amount
于 2012-09-08T02:47:29.487 回答