【问题标题】:Identifying the first occurrence of a state change in the TfsWarehouse DB识别 TfsWarehouse DB 中第一次发生的状态变化
【发布时间】:2018-04-22 21:17:54
【问题描述】:

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

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

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

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

它也可能再次失败。 TfsWarehouse 将这些更改记录为修订。我遇到的问题是对错误所做的任何更改也算作修订 - 这可能是对另一个字段(例如标题、描述等)的更改。每个修订都会在表中创建一条记录,因此存在以下实例具有相同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 and System_State = Committed”的第一个实例时,ntocomseq 列将有一个 1(有点像一个标志)这是我的结果:

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

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

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

【问题讨论】:

  • 如果你能给出一个小样本数据,也许对于单个工作项,这会更容易解决。您的 cte 结果没有多大帮助,因为看不到原始数据的样子。解释很好,但样本数据和预期输出对于 MCVE 至关重要。
  • 感谢您的回复和好点。我继续并添加了样本数据的捕获。这与我的原始示例中描述的工作项相同。
  • 酷@dougie88。您可以将其粘贴为文本以便我们将其复制到测试环境中吗?图片不太友好,哈哈。
  • 格式不好见谅

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


【解决方案1】:

好的,我想我在关注你。让我知道这是否能让你到达你想去的地方。由于您是 2012 年,我们可以使用 LEADLAG。在下面的 sn-p 中,我将创建两个额外的列。他们所做的是在该行根据前一行或下一行发生更改时进行标记。因此,从某种意义上说,本质上是“重复数据删除”。首先按原样运行并关注 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

【讨论】:

  • 哇,好用!非常好的工作,我感谢你的帮助!我现在可以做的是更改那些原始案例语句并将它们设置为每个单独状态更改的附加标志 - 即对于 Resolved to Failed 它将是 char F 或类似性质的东西。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 2022-07-15
  • 1970-01-01
  • 2022-01-16
  • 2021-04-21
相关资源
最近更新 更多