Приведенный ниже запрос использует базу данных Axosoft OnTime с несколькими настраиваемыми полями. Если вы игнорируете пользовательские поля и у вас есть примеры данных, это должно сработать.
Что я пытаюсь сделать:
Возврат запроса с количеством (общим) количеством открытых заявок, открытых за эти периоды времени:
- Меньше дня
- 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