我创建了一个脚本,该脚本使用以下脚本显示每月有多少独立人员解决了团队中的呼叫。
是否有办法找出个人身份,但仍根据我已经使用的脚本显示每个团队的每月数字?
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
答案 0 :(得分:0)
您有两种选择
这里都是用于实现选项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