如何按月显示团队中的单个解析器

how to display individual resolvers within a team on a monthly basis

我目前有一个 SQL 视图,显示自 2017 年 4 月以来每个团队每天有多少成员使用以下代码制定了决议。我将如何修改它以便我可以按月而不是每天来做?

SELECT CAST(stat_datetimeresolved as date) as DateResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
 WHERE Stat_DateTimeResolved >= '20170401'
GROUP BY CAST(Stat_DateTimeResolved as DATE), ResolvedByTeam
ORDER BY CAST(stat_datetimeresolved as DATE) asc, ResolvedByTeam

您可以将日期格式化为仅显示年月:

select format(getdate(), 'yyyyMM')

然后按这个值分组。所以你的查询应该变成这样:

SELECT FORMAT(stat_datetimeresolved, 'yyyyMM') as MonthResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
 WHERE Stat_DateTimeResolved >= '20170401'
GROUP BY FORMAT(stat_datetimeresolved, 'yyyyMM'), ResolvedByTeam
ORDER BY MonthResolved asc, ResolvedByTeam

更新:对于 2012 之前的版本,可以使用 DATEPART 函数构造年月部分。在这种情况下,查询可能如下所示:

SELECT CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) as MonthResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
 WHERE stat_datetimeresolved >= '20170401'
GROUP BY CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2), ResolvedByTeam
ORDER BY MonthResolved asc, ResolvedByTeam

SELECT convert(char(6), Stat_DateTimeResolved , 112) as MonthResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
 WHERE Stat_DateTimeResolved >= '20170401'
GROUP BY convert(char(6), Stat_DateTimeResolved , 112), ResolvedByTeam
ORDER BY MonthResolved asc, ResolvedByTeam