我写了一个查询,返回一组代理的开放支持票数。查询当前返回的时间超过5天:
SELECT groupname,
assignee,
Count(*) AS [Tickets Older Than 5 Days]
FROM zendeskunclosedticketexport
WHERE ( groupname = 'DC Web Ops'
OR groupname = 'DC Maintenance'
OR groupname = 'DC QC1' )
AND ticketcreateddate < Dateadd(day, -5, Getdate())
GROUP BY groupname,
assignee
ORDER BY groupname,
Count(*) DESC
查询结果示例:
groupname assignee Tickets Older Than 5 Days
DC Maintenance Mubarak Ali 12
DC Maintenance Abirami Selvanathan 11
DC Maintenance Jemima Mohanraj 6
DC Maintenance Praveena Rajan 6
DC Maintenance Rahul Ramesh 6
DC Maintenance Saravanan Varadharajan 6
DC Web Ops Kasey Barber 16
DC Web Ops Jamie Raines 11
DC Web Ops Keith Washington 10
DC Web Ops Patricia Gomez Imvendorize 6
我一直试图做的还包括10天,30天等的列,但一直无法做到。我试图在select语句中编写一个子查询来返回相应天数的计数;如下所示:
SELECT groupname,
assignee [currentAssignee],
(SELECT Count(*) AS [Tickets Older Than 5 Days]
FROM zendeskunclosedticketexport
WHERE (
ticketcreateddate < Dateadd(day, -5, Getdate())
AND
assignee = [currentAssignee]
)
(SELECT Count(*) AS [Tickets Older Than 10 Days]
FROM zendeskunclosedticketexport
WHERE (
ticketcreateddate < Dateadd(day, -10, Getdate())
AND
assignee = [currentAssignee]
)
(SELECT Count(*) AS [Tickets Older Than 30 Days]
FROM zendeskunclosedticketexport
WHERE (
ticketcreateddate < Dateadd(day, -30, Getdate())
AND
assignee = [currentAssignee]
)
FROM zendeskunclosedticketexport
我在这里尝试做的是将外部查询中的受理人值传递给内部查询...我知道,因为我拥有它这不合法,但希望它提供了一些关于方法的背景我在拿。有没有办法以临时变量的形式将数据从外部查询传递到内部查询?
编辑:用户通知我,我错过了一个FROM子句,之后添加了这个。
答案 0 :(得分:4)
您可以使用SUM
CASE
语句更简单地执行此操作:
SELECT
groupname,
assignee,
SUM(CASE WHEN ticketcreateddate < DATEADD(DAY, -5, GETDATE()) THEN 1 ELSE 0 END) AS [Tickets Older Than 5 Days],
SUM(CASE WHEN ticketcreateddate < DATEADD(DAY, -10, GETDATE()) THEN 1 ELSE 0 END) AS [Tickets Older Than 10 Days],
SUM(CASE WHEN ticketcreateddate < DATEADD(DAY, -30, GETDATE()) THEN 1 ELSE 0 END) AS [Tickets Older Than 30 Days]
FROM
zendeskunclosedticketexport
WHERE
groupname IN ('DC Web Ops', 'DC Maintenance', 'DC QC1')
GROUP BY
groupname,
assignee
ORDER BY
groupname
您可以将ORDER BY
设置为您需要的那些聚合列中的任何一个。