0

我有一个看起来像这样的表:

+--------+-------+--------+
|测试名称|测试运行|结果|
+--------+-------+--------+
| 测试1 | 1 | 失败 |
+--------+-------+--------+
| 测试1 | 2 | 失败 |
+--------+-------+--------+
| 测试2 | 1 | 失败 |
+--------+-------+--------+
| 测试2 | 2 | 通行证 |
+--------+-------+--------+
| 测试3 | 1 | 通行证 |
+--------+-------+--------+
| 测试3 | 2 | 失败 |
+--------+-------+--------+

该表用于存储测试结果的简要摘要。我想编写一个查询(使用 T-SQL,但任何方言都可以),返回每个测试失败的构建次数。使用示例作为输入,它应该返回如下结果集:

+--------+----------+
|测试名称|回归|
+--------+----------+
| 测试1 | 2 |
+--------+----------+
| 测试2 | 0 |
+--------+----------+
| 测试3 | 1 |
+--------+----------+

请注意,查询应该只计算当前的“失败连续”而不是计算失败的总数。可以假设 MAX(TestRun) 是最近的运行。

有任何想法吗?

编辑:语法

4

3 回答 3

3

有点难看,但它的工作原理。

create table dbo.tests
(
    TestName nvarchar(10) not null
    , TestRun int not null
    , OutCome nvarchar(10) not null
)

insert into dbo.tests
select 'Test1', 1, 'Fail'
union all 
select 'Test1', 3, 'Fail'
union all 
select 'Test2', 1, 'Fail'
union all 
select 'Test2', 3, 'Pass'
union all 
select 'Test3', 1, 'Pass'
union all 
select 'Test3', 3, 'Fail'

; with c (TestName, TestRun, OutCome, TestId)
as
(
    select TestName, TestRun, OutCome
            , dense_rank() over (order by TestRun) as TestId
    from dbo.tests
) 

select t.TestName, max(t.TestId) - mt.MaxPassedTestRun as FailedRuns
from c t
        inner join
            (
                select TestName, sum(TestId) as MaxPassedTestRun
                from
                    (
                        select TestName, TestId 
                        from c
                        where OutCome = 'Pass'

                        union all 

                        select TestName, 0 as TestRun
                        from c
                        where OutCome = 'Fail'
                    ) mt
                group by mt.TestName
            ) mt on t.TestName = mt.TestName
group by t.TestName, MaxPassedTestRun, mt.MaxPassedTestRun
于 2013-08-15T18:46:31.203 回答
1
select a.TestName,a.TestRun,a.Outcome From table1 a join 
Table1 b on a.TestName=b.testname          
and a.Outcome='Fail' 
and a.TestRun>b.TestRun
于 2013-08-15T18:02:44.650 回答
1

这是使用 CTE 的另一种方法:

;WITH streaks AS (
    SELECT t.Name, t.Run, t.Outcome 
    FROM TestRuns t
    INNER JOIN (SELECT Name, MAX(Run) AS MaxRun FROM TestRuns GROUP BY Name) maxes
    ON maxes.Name = t.Name AND maxes.MaxRun = t.Run

    UNION ALL

    SELECT t.Name, t.Run, t.Outcome
    FROM TestRuns t
    INNER JOIN streaks s ON s.Name = t.Name AND s.Outcome = 0
    WHERE t.Run = s.Run - 1 AND t.Outcome = 0
)

SELECT Name
, SUM(CASE WHEN Outcome = 0 THEN 1 ELSE 0 END) AS Regression
FROM streaks
GROUP BY Name

这是一个带有数据样本的SQL Fiddle

于 2013-08-15T19:03:06.417 回答