Search code examples
sql-servervisual-studiotfsssmstfs-workitem

Getting TFS work items on SSMS


I need some help mimicking a TFS query. I have tried writing the following SQL query to do so but I am not able to get a matching population:

hh

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

IF OBJECT_ID('tempdb.dbo.#LastRecord') IS NOT NULL DROP TABLE #LastRecord 
SELECT System_Id
       , LastUpdatedDateTime = MAX(LastUpdatedDateTime) 
INTO #LastRecord
FROM [dimWorkItem]
GROUP BY System_Id 


SELECT distinct ID = w.System_Id
--Cannot find Department
       , Department = '?'
       , w.TeamProjectSK
       , RequestedByUser = w.Custom_RequestedBy
       , Title = w.System_Title  
       , [Priority] = w.Microsoft_VSTS_Common_Priority
       , Activity = w.Microsoft_VSTS_Common_Activity
       , da.AreaPath
       , [State] = w.System_State
       , Reason = w.System_Reason
       , ClosedDate = w.Microsoft_VSTS_Common_ClosedDate
       , ActivatedDate = Microsoft_VSTS_Common_ActivatedDate
       , DevHours = Custom_DevHours
       , CreatedDate = w.System_CreatedDate 
       , AssignedTo = dp.Name
       , [Week] = CASE WHEN (w.System_State = 'Active' OR Microsoft_VSTS_Common_ClosedDate >= dateadd(d,-7,CONVERT(Date,getdate(),1))) THEN 'Current' 
                       WHEN (w.System_State = 'Active' OR Microsoft_VSTS_Common_ClosedDate >= dateadd(d,-14,CONVERT(Date,getdate(),1)) AND Microsoft_VSTS_Common_ClosedDate < dateadd(d,-7,CONVERT(Date,getdate(),1))) THEN 'Last' 
                       ELSE 'Previous' END
       , TSRNumber = Custom_TSRNumber
FROM 

(Select * from (select *, rn = row_number() over(partition by w1.System_Id, w1.TeamProjectSK, w1.System_State order by w1.System_Rev desc)
      from dbo.DimWorkItem w1) d where rn = 1) w
JOIN #LastRecord l ON w.System_Id = l.System_Id AND w.LastUpdatedDateTime = l.LastUpdatedDateTime
LEFT OUTER JOIN dbo.DimArea da ON w.AreaSK = da.AreaSK
LEFT OUTER JOIN DimPerson dp ON w.System_AssignedTo__PersonSK = dp.PersonSK
WHERE System_WorkItemType = 'Task'
AND da.AreaPath like '\LOS\Reporting%'
AND w.Microsoft_VSTS_Common_Priority < 4 

Also if anyone knows where I could find the department field as I am not sure whether it is a custom field or not.

SQL Server 2014: 12.0

TFS: 12.0

Visual studio professional 2015: 14.0

Thank you in advance for your suggestions!


Solution

  • I found a solution although still not able to find the the Department field :

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
    
    IF OBJECT_ID('tempdb.dbo.#LastRecord') IS NOT NULL DROP TABLE #LastRecord 
    
    SELECT System_Id, LastChangedDate = MAX(System_ChangedDate) 
    INTO #LastRecord
    FROM [dimWorkItem]
    GROUP BY System_Id 
    
    DECLARE @FromDateWeek DATE 
    DECLARE @FromDateTwoWeek DATE 
    SET @FromDateWeek = dateadd(d,-7,CONVERT(Date,getdate(),1))
    SET @FromDateTwoWeek = dateadd(d,-14,CONVERT(Date,getdate(),1))
    --SELECT @FromDate
    
    SELECT      ID = w.System_Id
        --Cannot find Department
              , Department = '?'
              , w.TeamProjectSK
              , RequestedByUser = w.Custom_RequestedBy
              , Title = w.System_Title  
              , [Priority] = w.Microsoft_VSTS_Common_Priority
              , Activity = w.Microsoft_VSTS_Common_Activity
              , da.AreaPath
              , [State] = w.System_State
              , Reason = w.System_Reason
              , ClosedDate = w.Microsoft_VSTS_Common_ClosedDate
              , ActivatedDate = Microsoft_VSTS_Common_ActivatedDate
              , DevHours = Custom_DevHours
              , CreatedDate = w.System_CreatedDate 
              , AssignedTo = dp.Name
              , [Week] = CASE WHEN  w.Microsoft_VSTS_Common_ClosedDate >= @FromDateWeek OR w.System_State = 'Active' THEN 'Current' 
                              WHEN (w.Microsoft_VSTS_Common_ClosedDate >= @FromDateTwoWeek AND w.Microsoft_VSTS_Common_ClosedDate < @FromDateWeek) 
                                    OR w.Microsoft_VSTS_Common_ClosedDate < @FromDateWeek THEN 'Last' 
                              ELSE 'Previous' END
    FROM [dimWorkItem] w 
    JOIN #LastRecord l ON w.System_Id = l.System_Id AND w.System_ChangedDate = l.LastChangedDate
    LEFT OUTER JOIN DimPerson dp ON w.System_AssignedTo__PersonSK = dp.PersonSK
    LEFT OUTER JOIN dbo.DimArea da ON w.AreaSK = da.AreaSK
    
    WHERE 
           da.AreaPath LIKE '\LOS\Reporting%'
           AND System_WorkItemType = 'Task'
           AND (Microsoft_VSTS_Common_ClosedDate >= @FromDateTwoWeek
                  OR w.System_State = 'Active')
           AND ISNULL(Microsoft_VSTS_Common_Priority,0) < 4
           AND w.System_Reason <> 'Obsolete'