请耐心等待我,因为我刚开始深入研究SQL报告,这只是倾销数据的巨大跳跃。
以下是表模式的简要说明。 表名:CallLog 字段:callid,tracker,recvddate,callsource
我使用这个简单的分组查询取得了成功
Select
right('00' + rtrim(Datepart(ww, recvddate)),2) week,
Tracker,
count(callsource) as [Email Tickets]
from
calllog
where datepart(year,recvddate) >= '2017' and callsource = 'email'
group by right('00' + rtrim(Datepart(ww, recvddate)),2),Tracker
order by 2,1
这给了我:
我试图生成一个报告,其中我将每个callource类型的记录数量计算为结果查询中的自己的列。
这就是我尝试过的:
Select
right('00' + rtrim(Datepart(ww, c1.recvddate)),2) week,
c1.Tracker,
count(c1.callsource) as [Email Tickets],
c2.[Queue Tickets]
from
calllog c1
inner join
(
Select
right('00' + rtrim(Datepart(ww, recvddate)),2) week,
Tracker,
count(callsource) as [Queue Tickets]
from
calllog
where datepart(year,recvddate) >= '2017' and callsource = 'phone'
group by right('00' + rtrim(Datepart(ww, recvddate)),2),Tracker
) c2
on c1.callid = c2.callid
where datepart(year,recvddate) >= '2017' and callsource = 'email'
group by right('00' + rtrim(Datepart(ww, recvddate)),2),Tracker
order by 2,1
但我收到以下错误。我试过改变我引用列的方式,但我没有尝试过任何工作。
任何建议表示赞赏。
更新:
在@Simon的帮助下,我能够提出这个我正在寻找的查询。
SELECT A.[week] as [Week],
A.tracker as Tracker,
B.Tickets as [Queue Tickets],
C.Tickets as [Email Tickets],
A.Tickets as [Total Tickets]
FROM (SELECT RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2) [week],
Tracker,
COUNT(callid) as Tickets
FROM calllog
WHERE DATEPART(year,recvddate) >= '2017' and tracker = 'djtaylor'
GROUP BY RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2), Tracker) as A
INNER JOIN (SELECT RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2) [week],
Tracker,
COUNT(callid) as Tickets
FROM calllog
WHERE DATEPART(year,recvddate) >= '2017' and callsource = 'queue'
GROUP BY RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2), Tracker) as B ON A.[week] = B.[week]
AND A.Tracker = B.Tracker
INNER JOIN (SELECT RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2) [week],
Tracker,
COUNT(callid) as Tickets
FROM calllog
WHERE DATEPART(year,recvddate) >= '2017' and callsource = 'email'
GROUP BY RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2), Tracker) as C ON A.[week] = C.[week]
AND A.Tracker = C.Tracker
ORDER BY 2,1
答案 0 :(得分:1)
我认为这就是你想要的:
Select
right('00' + rtrim(Datepart(ww, recvddate)),2) [week],
Tracker,
callsource,
count(callid) as [Email Tickets]
from
calllog
where datepart(year,recvddate) >= '2017' and callsource = 'email'
group by right('00' + rtrim(Datepart(ww, recvddate)),2), Tracker, callsource
order by [week], Tracker
行。我想这就是你想要的:
SELECT A.[week] as [Week],
A.tracker as Tracker,
A.callsource,
A.Tickets as CallSourceTickets,
B.Tickets CallCenterTickets
FROM (SELECT RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2) [week],
Tracker,
callsource,
COUNT(callid) as Tickets
FROM calllog
WHERE DATEPART(year,recvddate) >= '2017'
GROUP BY RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2), Tracker, callsource) as A
INNER JOIN (SELECT RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2) [week],
Tracker,
COUNT(callid) as Tickets
FROM calllog
WHERE DATEPART(year,recvddate) >= '2017'
GROUP BY RIGHT('00' + RTRIM(DATEPART(ww, recvddate)),2), Tracker) as B ON A.[week] = B.[week]
AND A.Tracker B.Tracker
ORDER BY [Week], Tracker