我可以找到每月唯一的事件解决者吗?
can I find out unique resolvers of incidents per month?
我创建了一个脚本,使用以下脚本显示团队中每个月有多少独特的人解决了呼叫。
有没有办法找出个人是谁,但仍然根据我已经在使用的脚本显示每个团队的月度数字?
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
你有两个选择
- 为了扩展您的结果,return 为 MonthResolved、ResolvedByTeam 和 resolvedby 的每个唯一组合单独一行。这将比原始查询多 return 行,因为在一个 month/team 组合中,您将获得与解析器一样多的行。
- 向您的查询添加一列,其中 return 包含解析器名称的列表(例如逗号分隔)。这可以通过 STRING_AGG function, but it is available in SQL Server 2017 and newer only. For 2008 your option is to perform some ugly conversions using FOR XML 子句轻松完成。
这里有两个查询,用于实现选项 1 和 2:
;with cte1 as (
SELECT
CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) as MonthResolved
, ResolvedByTeam
, resolvedby
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, resolvedby
)
select
MonthResolved
, ResolvedByTeam
, (select count(distinct resolvedby) as Name
from [dbo].[Incident] i
where CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) = cte1.MonthResolved
and i.ResolvedByTeam = cte1.ResolvedByTeam)
, resolvedby
from cte1
ORDER BY MonthResolved asc, ResolvedByTeam, resolvedby
;with cte2 as (
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
)
select *
from cte2
cross apply (select (
select distinct resolvedby + ', ' as [text()]
from [dbo].[Incident]
where ResolvedByTeam = cte2.ResolvedByTeam
and CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) = cte2.MonthResolved
for xml path ('')) as Names) t
ORDER BY MonthResolved asc, ResolvedByTeam
我创建了一个脚本,使用以下脚本显示团队中每个月有多少独特的人解决了呼叫。
有没有办法找出个人是谁,但仍然根据我已经在使用的脚本显示每个团队的月度数字?
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
你有两个选择
- 为了扩展您的结果,return 为 MonthResolved、ResolvedByTeam 和 resolvedby 的每个唯一组合单独一行。这将比原始查询多 return 行,因为在一个 month/team 组合中,您将获得与解析器一样多的行。
- 向您的查询添加一列,其中 return 包含解析器名称的列表(例如逗号分隔)。这可以通过 STRING_AGG function, but it is available in SQL Server 2017 and newer only. For 2008 your option is to perform some ugly conversions using FOR XML 子句轻松完成。
这里有两个查询,用于实现选项 1 和 2:
;with cte1 as (
SELECT
CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) as MonthResolved
, ResolvedByTeam
, resolvedby
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, resolvedby
)
select
MonthResolved
, ResolvedByTeam
, (select count(distinct resolvedby) as Name
from [dbo].[Incident] i
where CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) = cte1.MonthResolved
and i.ResolvedByTeam = cte1.ResolvedByTeam)
, resolvedby
from cte1
ORDER BY MonthResolved asc, ResolvedByTeam, resolvedby
;with cte2 as (
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
)
select *
from cte2
cross apply (select (
select distinct resolvedby + ', ' as [text()]
from [dbo].[Incident]
where ResolvedByTeam = cte2.ResolvedByTeam
and CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) = cte2.MonthResolved
for xml path ('')) as Names) t
ORDER BY MonthResolved asc, ResolvedByTeam