1

下面的查询使用带有一些自定义字段的 Axosoft 的 OnTime 数据库。如果您忽略自定义字段并有一些示例数据,这应该可以工作。

我正在尝试做的事情:

返回一个查询,其中包含在这些时间范围内已打开的已打开工单的 count(total) 数量:

  • 不到一天
  • 1天
  • 2天
  • 3天
  • 4天
  • 5天
  • 6天
  • 一个多星期

这是用于票龄查询。这是下面的查询:

DECLARE @endDate DateTime;
SET @endDate = '03/18/2011';

WITH
      WorkItems AS
      (
            SELECT
                  i.ProjectID AS ProjectID,
                  CASE WHEN ic.Custom_279 = 'Bug' THEN 'Issue' WHEN ic.Custom_279 IS NULL THEN 'Other' WHEN LTRIM(RTRIM(ic.Custom_279)) = '' THEN 'Other' ELSE ic.Custom_279 END  AS WorkItemType,
                  i.IncidentNumber AS ID,
                  i.Name AS Name,
                  --CASE WHEN ic.Custom_264 < '1901-01-01' THEN NULL ELSE ic.Custom_264 END AS DateReported,
                  CASE WHEN i.CreatedDateTime < '1901-01-01' THEN NULL ELSE i.CreatedDateTime END AS DateReported,
                  CASE WHEN ic.Custom_265 < '1901-01-01' THEN NULL ELSE ic.Custom_265 END AS DateResolutionBegan,
                  CASE WHEN ic.Custom_266 < '1901-01-01' THEN NULL ELSE ic.Custom_266 END AS DateSignoffRequested,
                  CASE WHEN ic.Custom_267 < '1901-01-01' THEN NULL ELSE ic.Custom_267 END AS DateClosed             
            FROM
                  dbo.Incidents AS i
                        INNER JOIN dbo.IncidentCustomFields AS ic ON ic.IncidentID = i.IncidentID
            WHERE
                  i.Archived = 0
      ),
      ProjectDescendantsIncludingSelf AS
      (
            SELECT
                    p.ProjectID AS ProjectID,
                    p.ProjectID AS DescendantProjectID,
                    CAST('/' + p.Name AS NVARCHAR) AS ProjectPath
            FROM
                    dbo.Projects AS p
            UNION ALL
            SELECT
                    pd.ProjectID AS ProjectID,
                    p.ProjectID AS DescendantProjectID,
                    CAST(pd.ProjectPath + N'/' + p.Name AS NVARCHAR) AS ProjectPath
            FROM
                    ProjectDescendantsIncludingSelf AS pd
                              INNER JOIN dbo.Projects AS p ON p.ParentID = pd.DescendantProjectID                        
      ),
    OpenTicketsLessThanDay AS   
        (SELECT 
            COUNT(ID) AS [LessThanDayTicketCount],
            CASE WHEN DATEDIFF(DAY, DateReported, @endDate) < 1 THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [LessThanDay],
            --CASE WHEN (DATEDIFF(DAY, DateReported, @endDate) > 1) AND (DATEDIFF(DAY, DateReported, @endDate) <= 2) THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [GreaterThan1Day],
            --CASE WHEN (DATEDIFF(DAY, DateReported, @endDate) > 2) AND (DATEDIFF(DAY, DateReported, @endDate) <= 3) THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [GreaterThan2Days],
            --CASE WHEN (DATEDIFF(DAY, DateReported, @endDate) > 3) AND (DATEDIFF(DAY, DateReported, @endDate) <= 4) THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [GreaterThan3Days],
            --CASE WHEN (DATEDIFF(DAY, DateReported, @endDate) > 4) AND (DATEDIFF(DAY, DateReported, @endDate) <= 5) THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [GreaterThan4Days],
            --CASE WHEN (DATEDIFF(DAY, DateReported, @endDate) > 5) AND (DATEDIFF(DAY, DateReported, @endDate) <= 6) THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [GreaterThan5Days],
            --CASE WHEN (DATEDIFF(DAY, DateReported, @endDate) > 6) AND (DATEDIFF(DAY, DateReported, @endDate) <= 7) THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [GreaterThan6Days],
            --CASE WHEN DATEDIFF(WEEK, DateReported, @endDate) > 1 THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [GreaterThanWeek],
            --DateReported,
            --DateClosed, 
            --d.ProjectPath

    FROM WorkItems wi
        INNER JOIN ProjectDescendantsIncludingSelf d ON d.DescendantProjectID = wi.ProjectID
    WHERE 
        DateReported < @endDate AND
        (DateClosed IS NULL OR DateClosed > @endDate) AND
        d.ProjectID = 182 AND
        d.DescendantProjectID != 185
    GROUP BY LessThanDay

    )

SELECT [LessThanDayTicketCount] FROM OpenTicketsLessThanDay
GROUP BY LessThanDay
ORDER BY LessThanDay ASC
4

1 回答 1

0

我认为您需要其中一种或另一种。

DECLARE @EndDate datetime = getdate()

SELECT 
      COUNT(CASE WHEN modify_date > DATEADD(DAY,-1,@EndDate)  THEN 1 END) AS [LessThanDay],
      COUNT(CASE WHEN modify_date > DATEADD(DAY,-2,@EndDate) AND modify_date <= DATEADD(DAY,-1,@EndDate) THEN 1 END) AS [GreaterThan1Day],
      COUNT(CASE WHEN modify_date > DATEADD(DAY,-3,@EndDate) AND modify_date <= DATEADD(DAY,-2,@EndDate) THEN 1 END) AS [GreaterThan2Days],
      COUNT(CASE WHEN modify_date > DATEADD(DAY,-4,@EndDate) AND modify_date <= DATEADD(DAY,-3,@EndDate) THEN 1 END) AS [GreaterThan3Days],
      COUNT(CASE WHEN modify_date > DATEADD(DAY,-5,@EndDate) AND modify_date <= DATEADD(DAY,-4,@EndDate) THEN 1 END) AS [GreaterThan4Days],
      COUNT(CASE WHEN modify_date > DATEADD(DAY,-6,@EndDate) AND modify_date <= DATEADD(DAY,-5,@EndDate) THEN 1 END) AS [GreaterThan5Days],
      COUNT(CASE WHEN modify_date > DATEADD(DAY,-7,@EndDate) AND modify_date <= DATEADD(DAY,-6,@EndDate) THEN 1 END) AS [GreaterThan6Days],
      COUNT(CASE WHEN modify_date <= DATEADD(DAY,-7,@EndDate) THEN 1 END) AS [GreaterThanWeek]
FROM sys.objects
WHERE modify_date <= @EndDate


SELECT 
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) = 0  THEN 1 END) AS [LessThanDay],
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) = 1  THEN 1 END) AS [GreaterThan1Day],
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) = 2  THEN 1 END) AS [GreaterThan2Days],
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) = 3  THEN 1 END) AS [GreaterThan3Days],
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) = 4  THEN 1 END) AS [GreaterThan4Days],
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) = 5  THEN 1 END) AS [GreaterThan5Days],
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) = 6  THEN 1 END) AS [GreaterThan6Days],
      COUNT(CASE WHEN DATEDIFF(DAY, modify_date, @EndDate) > 6  THEN 1 END) AS [GreaterThanWeek]
FROM sys.objects
WHERE modify_date <= @EndDate
于 2011-03-21T20:01:05.607 回答