识别TfsWarehouse DB

时间:2017-11-09 18:41:15

标签: sql sql-server tfs sql-server-2012 azure-pipelines

您正在寻求以下方面的帮助,我正在编写一份报告,以便我可以区分错误生命周期中何时发生错误。我们当前的流程存在以下错误:

  

新>承诺>进行中>构建>已解决>完成

如果发生故障,则错误将从已解决:

  

失败>进行中>构建>已解决>完成

它也可能再次失败。 TfsWarehouse将这些更改记录为修订版。我遇到的问题是对bug的任何更改也算作修订 - 这可能是对标题,描述等其他字段的更改。每个修订都在表中创建一条记录,所以有具有相同PreviousStateSystem_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_number() over(partition)函数来执行此操作。当第一个实例是' Previous State = New和System_State = Committed'遇到然后列ntocomseq将有一个1(有点像一个标志)这是我的结果:

enter image description here

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

我遇到的问题是当一个项目再次失败并且被修复时,查询没有捕获它。即如果PreviousState=InProgressSystem_State=Build第二次不给它1,它实际上在某些情况下得到2。有没有其他方法来解决这个问题?有没有其他人使用TfsWarehouse db表遇到类似的问题?

我想得到的是上面的结果,但是第二次从In ProgressBuild的错误实例。我的分区可能存在问题,因为我要对整个工作项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

1 个答案:

答案 0 :(得分:1)

好的,我想我跟着你。让我知道如果这就是你想要的地方。自2012年起,我们可以使用LEADLAG。在下面的代码段中,我将创建两个额外的列。他们所做的是基于前一行或下一行更改行时的标记。因此,从某种意义上说,基本上是“重复数据删除”您的数据。首先运行它,然后关注CurrentState列和我添加的两列。然后,您可以使用我已注释掉的相应where子句将它们限制为您想要的行。

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