Sql 识别TfsWarehouse数据库中第一次出现的状态更改

Sql 识别TfsWarehouse数据库中第一次出现的状态更改,sql,sql-server,tfs,sql-server-2012,azure-pipelines,Sql,Sql Server,Tfs,Sql Server 2012,Azure Pipelines,您好,我正在寻求以下方面的帮助,我正在编写一份报告,该报告将允许我区分在错误生命周期中何时出现错误故障。我们当前的流程存在以下错误: 新建>提交>进行中>生成>已解决>完成 如果出现故障,错误将从已解决变为: 失败>进行中>生成>已解决>完成 它也可能再次失败。TfsWarehouse将这些更改记录为修订。我遇到的问题是,对bug所做的任何更改也被视为修订-这可能是对另一个字段的更改,例如标题、描述等。每个修订都会在表中创建一条记录,因此存在多条记录的实例,这些记录具有相同的PreviousSt

您好,我正在寻求以下方面的帮助,我正在编写一份报告,该报告将允许我区分在错误生命周期中何时出现错误故障。我们当前的流程存在以下错误:

新建>提交>进行中>生成>已解决>完成

如果出现故障,错误将从已解决变为:

失败>进行中>生成>已解决>完成

它也可能再次失败。
TfsWarehouse
将这些更改记录为修订。我遇到的问题是,对bug所做的任何更改也被视为修订-这可能是对另一个字段的更改,例如标题、描述等。每个修订都会在表中创建一条记录,因此存在多条记录的实例,这些记录具有相同的
PreviousState
System\u State
条目。我能够编写一个查询,以区分状态更改的第一个实例并消除重复的实例,请参见下文:

WITH myTable (WorkItem, PreviousState, ChangedDate, ChangedBy, ID, Title, CurrentState, RevisionNo, Reason, CreatedDate, toNew, toCommited, toIP, toBuild, toResolved, toDone, toFailed, FailedtoIP)

AS (
SELECT
      [WorkItem]
      ,[PreviousState]
      ,[System_ChangedDate]
      ,dp.Name as 'Changed By'
      ,[System_Id]
      ,[System_Title]
      ,[System_State]
      ,[System_Rev]
      ,[System_Reason]
      ,[System_CreatedDate]
    ,row_number() over (partition by System_Id 
                                order by (case when PreviousState IS NULL AND System_State = 'New' then 1 else 2 end), System_ChangedDate asc) as newseq
    ,row_number() over (partition by System_Id
                                order by (case when PreviousState = 'New' AND System_State = 'Committed' then 1 else 2 end), System_ChangedDate asc) as ntocomseq
    ,row_number() over (partition by System_Id
                                order by (case when PreviousState = 'Committed' AND System_State = 'In Progress' then 1 else 2 end), System_ChangedDate asc) as ctoipseq
    ,row_number() over (partition by System_Id
                                order by (case when PreviousState = 'In Progress' AND System_State = 'Build' then 1 else 2 end), System_ChangedDate asc) as iptobseq
    ,row_number() over (partition by System_Id
                                order by (case when PreviousState = 'Build' AND System_State = 'Resolved' then 1 else 2 end), System_ChangedDate asc) as btoresseq
    ,row_number() over (partition by System_Id
                                order by (case when PreviousState = 'Resolved' AND System_State = 'Done' then 1 else 2 end), System_ChangedDate asc) as restodseq
    ,row_number() over (partition by System_Id
                                order by (case when PreviousState = 'Resolved' AND System_State = 'Failed' then 1 else 2 end), System_ChangedDate asc) as restofseq
    ,row_number() over (partition by System_Id
                                order by (case when PreviousState = 'Failed' AND System_State = 'In Progress' then 1 else 2 end), System_ChangedDate asc) as ftoipseq
FROM [Tfs_Warehouse].[dbo].[vDimWorkItemOverlay] as dwi1
    LEFT JOIN [Tfs_Warehouse].[dbo].[DimPerson] AS dp  
    ON dwi1.System_ChangedBy__PersonSK = dp.PersonSK

  WHERE System_Id IN (36708

        --SELECT DISTINCT System_Id

        --FROM 
        --  [Tfs_Warehouse].[dbo].[vDimWorkItemOverlay] AS dwi2

        --WHERE System_State IN ('Failed')
        --AND IterationLevel0 = 'v7'
        --AND YEAR(System_CreatedDate) = 2017 
        )

  )

  SELECT *

  FROM myTable
  Where toNew = 1 or toCommited = 1 or toIP = 1 or toBuild = 1 or toResolved = 1 or toDone = 1 or toFailed = 1 or FailedtoIP = 1
  Order by ID, RevisionNo
上面所做的是使用一个CTE,基本上在任何状态更改的第一个实例中添加一个1。它使用
row\u number()覆盖(分区)
函数来执行此操作。当遇到“Previous State=New and System_State=Committed”的第一个实例时,列
ntocomseq
将有一个1(有点像一个标志),下面是我的结果:

其中的子查询已被注释掉,但所做的只是将可能的工作项列表进一步过滤到那些仅失败过的工作项

我遇到的问题是,当一个项再次失败并得到修复时,查询无法捕获它。也就是说,如果第二次
PreviousState=InProgress
System\u State=Build
它将不会得到1,在某些情况下它实际上会得到2。还有别的办法解决这个问题吗?使用
TfsWarehouse
db表的其他人是否遇到类似问题

我想得到的是上面的结果,但是随着错误实例从
进行中
构建
第二次出现。我的分区可能有问题,因为我正在对整个工作项ID进行分区,所以它只查找状态更改的第一个实例,但只查找一次。每次我都需要第一次的改变

如果您有任何问题或需要进一步解释,请告诉我,谢谢!希望不会太混乱

这是一个示例数据——我刚刚选择了我在CTE中使用的列

WorkItem PreviousState  System_ChangedDate  System_Id   System_Title    System_State    System_Rev  System_Reason   System_CreatedDate

<!-- -->

Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor NULL           2017-07-19 15:40:12.873  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   New 1       New defect reported                     2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor New            2017-07-20 07:32:26.800  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Committed   2       Commitment made by the team 2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Committed      2017-07-20 07:32:28.907  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   In Progress 3       Work started                2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor In Progress    2017-07-20 07:32:31.660  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Build       4       Build pending                   2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Build          2017-07-20 07:32:34.410  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Resolved    5       Fixed                       2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Build          2017-07-20 07:33:55.623  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Resolved    6       Fixed                       2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Build          2017-07-20 12:09:26.707  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Resolved    7       Fixed                       2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-07-20 12:09:54.177  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Failed      8       Bug not fixed                   2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-07-20 12:10:17.037  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Failed      9   Bug not fixed                   2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-07-20 12:12:53.960  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Failed      10      Bug not fixed                   2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Failed         2017-07-21 07:26:40.930  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   In Progress 11   Work started               2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Failed         2017-07-24 07:36:44.370  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   In Progress 12      Work started                2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor In Progress    2017-07-24 10:16:37.360  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Build       13      Build pending                   2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor In Progress    2017-07-24 10:16:45.373  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Build       14      Build pending                   2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Build          2017-07-24 10:16:57.720  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Resolved    15      Fixed                       2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Build          2017-07-24 10:17:38.133  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Resolved    16      Fixed                       2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-07-24 10:17:44.010  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Done 17 Resolution accepted     2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-07-25 15:25:36.490  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Done 18     Resolution accepted                     2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-08-11 13:54:08.960  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Done 19     Resolution accepted                     2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-10-10 15:09:32.593  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Done 20     Resolution accepted                     2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-10-10 15:33:41.343  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Done 21     Resolution accepted                     2017-07-19 15:40:12.873
Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor Resolved       2017-10-10 15:35:01.910  36708   MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor   Done 22     Resolution accepted                     2017-07-19 15:40:12.873
工作项目先前状态系统\u变更日期系统\u Id系统\u标题系统\u状态系统\u修订系统\u原因系统\u创建日期
Bug 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的形状尺寸NULL 2017-07-19 15:40:12.873 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的形状尺寸新报告1个新缺陷      2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸New 2017-07-20 07:32:26.800 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸由团队2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸提交2017-07-20 07:32:28.907 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸正在进行3工作已开始  2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在进行中的详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸2017-07-20 07:32:31.660 36708 MEP Reno:可以在详细编辑器构建4构建挂起中编辑子平台视图的样式组和孙子平台视图的表单尺寸      2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在详细编辑器构建2017-07-20 07:32:34.410中编辑子平台视图的样式组和孙子平台视图的表单尺寸。36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸  2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在详细编辑器构建2017-07-20 07:33:55.623 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙辈平台视图的表单尺寸解决了6个固定问题  2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在详细编辑器构建2017-07-20 12:09:26.707 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸  2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸已解决2017-07-20 12:09:54.177 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸失败8 Bug未修复      2017-07-19 15:40:12.873
Bug 36708 MEP Reno:可以在详细编辑器中编辑子平台视图的样式组和孙子平台视图的表单尺寸解析2017-07-20 12:10:17.037 36708 MEP Reno:可以编辑子平台视图的样式组和孙子平台视图的表单尺寸
declare @table table(
                        WorkItem varchar(4000)
                        ,PreviousState varchar(256) null
                        ,ChangedDate datetime2
                        ,ID int
                        ,Title varchar(4000)
                        ,CurrentState varchar(256)
                        ,RevisionNo int
                        ,Reason varchar(256)
                        ,CreatedDate datetime2)
insert into @table
values
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor',NULL,'2017-07-19 15:40:12.873',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','New',1,'New defect reported','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','New','2017-07-20 07:32:26.800',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Committed',2,'Commitment made by the team','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Committed','2017-07-20 07:32:28.907',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','In Progress',3,'Work started','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','In Progress','2017-07-20 07:32:31.660',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build',4,'Build pending','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build','2017-07-20 07:32:34.410',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved',5,'Fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build','2017-07-20 07:33:55.623',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved',6,'Fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build','2017-07-20 12:09:26.707',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved',7,'Fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-07-20 12:09:54.177',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Failed',8,'Bug not fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-07-20 12:10:17.037',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Failed',9,'Bug not fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-07-20 12:12:53.960',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Failed',10,'Bug not fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Failed','2017-07-21 07:26:40.930',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','In Progress',11,'Work started','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Failed','2017-07-24 07:36:44.370',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','In Progress',12,'Work started','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','In Progress','2017-07-24 10:16:37.360',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build',13,'Build pending','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','In Progress','2017-07-24 10:16:45.373',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build',14,'Build pending','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build','2017-07-24 10:16:57.720',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved',15,'Fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Build','2017-07-24 10:17:38.133',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved',16,'Fixed','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-07-24 10:17:44.010',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Done',17,'Resolution accepted','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-07-25 15:25:36.490',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Done',18,'Resolution accepted','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-08-11 13:54:08.960',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Done',19,'Resolution accepted','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-10-10 15:09:32.593',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Done',20,'Resolution accepted','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-10-10 15:33:41.343',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Done',21,'Resolution accepted','2017-07-19 15:40:12.873'),
('Bug 36708 MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Resolved','2017-10-10 15:35:01.910',36708,'MEP Reno: Can edit stylegroup of child platform views and form dimensions of grandchild platform views in detailed editor','Done',22,'Resolution accepted','2017-07-19 15:40:12.873')


;with cte as(
select
    *
    ,RevisionNumber = row_number() over (partition by ID order by ChangedDate)
    ,FirstInstance = case when lag(CurrentState) over (partition by ID order by ChangedDate) = CurrentState then 0 else 1 end
    ,LastInstance = case when lead(CurrentState) over (partition by ID order by ChangedDate) = CurrentState then 0 else 1 end
from @table)

select
    *
from cte
order by ID, ChangedDate
--where FirstInstance = 1
--where LastInstance = 1