我正在运行查询,尝试按星期几获取一些指标以及总数。我使用ROLLUP来获得本周的总平均值。但是,当我这样做时,我看到结果加倍。数字是相同的,我确认它们是正确的。但是,我想让它只在星期六到星期六生产,下面列出了总计。有没有办法重写这个查询呢?
select DATEPART(dw,t1.Call), DATENAME(dw,t1.Call),
AVG (T1.CallSec + T1.ACWSec) AS AHT
from bm.calls t1 with (nolock)
JOIN prc.RRequest rreq
on t1.callid = rreq.t1CallID
join PRC.RRaw rraw with (nolock)
on rreq.RoutingID = rraw.RoutingId
where t1.CallDate >= '2014-01-01'
AND rraw.RMode='Active'
GROUP BY ROLLUP (DATEPART(dw,t1.Call)),(DATENAME(dw,t1.Call) )
这是我得到的输出
NULL Monday 367.70605696962
NULL Sunday 335.661825117371
NULL Tuesday 359.261558682542
NULL NULL 353.421450966283
NULL Friday 361.478009630819
NULL Saturday 354.216226822329
NULL Thursday 330.073051380636
NULL Wednesday 345.357746478873
1 Sunday 335.661825117371
2 Monday 367.70605696962
3 Tuesday 359.261558682542
4 Wednesday 345.357746478873
5 Thursday 330.073051380636
6 Friday 361.478009630819
7 Saturday 354.216226822329
所需的输出应
1 Sunday 335.661825117371
2 Monday 367.70605696962
3 Tuesday 359.261558682542
4 Wednesday 345.357746478873
5 Thursday 330.073051380636
6 Friday 361.478009630819
7 Saturday 354.216226822329
NULL NULL 353.421450966283
答案 0 :(得分:0)
我认为您只需要更改ROLLUP
以包含这两个字段:
SELECT DATEPART(dw, t1.Call)
, DATENAME(dw, t1.Call)
, AVG(T1.CallSec + T1.ACWSec) AS AHT
FROM bm.calls t1 WITH ( NOLOCK )
JOIN prc.RRequest rreq ON t1.callid = rreq.t1CallID
JOIN PRC.RRaw rraw WITH ( NOLOCK ) ON rreq.RoutingID = rraw.RoutingId
WHERE t1.CallDate >= '2014-01-01'
AND rraw.RMode = 'Active'
GROUP BY ROLLUP((DATEPART(dw, t1.Call), DATENAME(dw, t1.Call)))
ROLLUP()
将根据所包含的字段集添加小计/总计行,您可以将多个字段合并为一组,方法是将它们包装在括号中。
所以ROLLUP(col1,col2)
除了总计之外,还会为col2
中的每次更改生成小计。而ROLLUP((col1,col2))
会将两列合并为一组,因此只显示一个总