将 Rollup 与 SQL 一起使用会产生双重结果
Using Rollup with SQL produces double results
我是 运行 一个查询,试图按星期几和总数获取一些指标。我正在使用 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
我相信您只需要更改 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()
将根据包含的字段集添加 subtotal/total 行,您可以通过将多个字段括在括号中将它们合并为一个集合。
因此 ROLLUP(col1,col2)
将导致 col2
中的每个变化的小计以及总计。而 ROLLUP((col1,col2))
会将两列合并为一组,因此仅显示总计。
我是 运行 一个查询,试图按星期几和总数获取一些指标。我正在使用 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
我相信您只需要更改 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()
将根据包含的字段集添加 subtotal/total 行,您可以通过将多个字段括在括号中将它们合并为一个集合。
因此 ROLLUP(col1,col2)
将导致 col2
中的每个变化的小计以及总计。而 ROLLUP((col1,col2))
会将两列合并为一组,因此仅显示总计。