在 SQL 中合并 select 语句
Unioning select statements in SQL
我正在尝试合并 SQL
中我是 运行 的 2 个查询
代码如下:
SELECT DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
AVG (T1.LengthSec + T1.Sec) AS Infl_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.ID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=1
GROUP BY ROLLUP ((DATEPART(dw,t1.Date),DATENAME(dw,t1.Date)))
Union ALL
select DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
AVG (T1.LengthSec + T1.Sec) AS Non_inf_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.t1CallID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=0
GROUP BY ROLLUP ((DATEPART(dw,t1.Date),DATENAME(dw,t1.Date)))
ORDER BY D1
但是,当我这样做时,它并没有完全创建我想要的输出
输出:
D1 Day_of_Week Infl_AHT
1 Sunday 207.783300685602
1 Sunday 245.439166666667
2 Monday 224.625025938991
2 Monday 261.060339364923
但输出应该更细化。应该是
D1 Day_of_Week Infl_AHT Non_infl_AHT
1 Sunday 207.783300685602 245.439166666667
2 Monday 224.625025938991 261.060339364923
我应该尝试创建表然后加入这个,还是实际上可以联合 select 语句?
我认为您可以使用 conditional aggregate
在单个查询中完成此操作。
试试这个。
SELECT Datepart(dw, t1.Date) AS D1,
Datename(dw, t1.Date) AS Day_of_Week,
Avg (CASE WHEN rreq.Influenced = 1 THEN T1.LengthSec + T1.Sec END) Infl_AHT,
Avg (CASE WHEN rreq.Influenced = 0 THEN T1.LengthSec + T1.Sec END) Non_inf_AHT
FROM bm.t1agent t1 WITH (nolock)
JOIN prc.Route rreq
ON t1.id = rreq.ID
JOIN PRC.Routing rraw WITH (nolock)
ON rreq.ID = rraw.Id
WHERE t1.Date >= '2014-12-07'
GROUP BY ROLLUP (( Datepart(dw, t1.Date), Datename(dw, t1.Date) ))
UNION
类似于垂直连接。它汇集了在其字段中具有相同内容的查询。
您要做的是水平连接两个具有不同字段的查询,这将通过 D1 或 Day_of_of_Week 字段上的标准 JOIN
来完成。
很可能正确地创建了输出。只是在 union 语句输出中,首先出现的相应匹配列的别名显示在输出中。请看以下声明:
select sysdate as a1 from dual
union all
select sysdate as a2 from dual;
请注意,在输出中,列名是 a1(尽管您在第二个 select 语句中将其别名为 a2)。
另请注意,日期显示在结果集中的两个不同行中(即使 sysdate 的值相同)。如果您使用 UNION 而不是 UNION all,则会过滤所有重复的列。
这比 NoDisplayName 的解决方案笨拙得多,我只是在演示一种替代方法。通过在每个联合部分中为聚合函数添加一个 "dummy" 列,您可以获得每个部分的不同平均列。我将汇总移动到外层:
select D1, Day_of_Week, max(Infl_AHT), max(Non_inf_AHT)
from (
SELECT DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
AVG (T1.LengthSec + T1.Sec) AS Infl_AHT,
-- add dummy column
null as Non_inf_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.ID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=1
GROUP BY DATEPART(dw,t1.Date), DATENAME(dw,t1.Date)
Union ALL
select DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
-- add dummy column
null as Infl_AHT,
AVG (T1.LengthSec + T1.Sec) AS Non_inf_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.t1CallID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=0
GROUP BY DATEPART(dw,t1.Date), DATENAME(dw,t1.Date)
)
GROUP BY ROLLUP (D1, Day_of_Week)
ORDER BY D1;
我正在尝试合并 SQL
中我是 运行 的 2 个查询代码如下:
SELECT DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
AVG (T1.LengthSec + T1.Sec) AS Infl_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.ID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=1
GROUP BY ROLLUP ((DATEPART(dw,t1.Date),DATENAME(dw,t1.Date)))
Union ALL
select DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
AVG (T1.LengthSec + T1.Sec) AS Non_inf_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.t1CallID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=0
GROUP BY ROLLUP ((DATEPART(dw,t1.Date),DATENAME(dw,t1.Date)))
ORDER BY D1
但是,当我这样做时,它并没有完全创建我想要的输出
输出:
D1 Day_of_Week Infl_AHT
1 Sunday 207.783300685602
1 Sunday 245.439166666667
2 Monday 224.625025938991
2 Monday 261.060339364923
但输出应该更细化。应该是
D1 Day_of_Week Infl_AHT Non_infl_AHT
1 Sunday 207.783300685602 245.439166666667
2 Monday 224.625025938991 261.060339364923
我应该尝试创建表然后加入这个,还是实际上可以联合 select 语句?
我认为您可以使用 conditional aggregate
在单个查询中完成此操作。
试试这个。
SELECT Datepart(dw, t1.Date) AS D1,
Datename(dw, t1.Date) AS Day_of_Week,
Avg (CASE WHEN rreq.Influenced = 1 THEN T1.LengthSec + T1.Sec END) Infl_AHT,
Avg (CASE WHEN rreq.Influenced = 0 THEN T1.LengthSec + T1.Sec END) Non_inf_AHT
FROM bm.t1agent t1 WITH (nolock)
JOIN prc.Route rreq
ON t1.id = rreq.ID
JOIN PRC.Routing rraw WITH (nolock)
ON rreq.ID = rraw.Id
WHERE t1.Date >= '2014-12-07'
GROUP BY ROLLUP (( Datepart(dw, t1.Date), Datename(dw, t1.Date) ))
UNION
类似于垂直连接。它汇集了在其字段中具有相同内容的查询。
您要做的是水平连接两个具有不同字段的查询,这将通过 D1 或 Day_of_of_Week 字段上的标准 JOIN
来完成。
很可能正确地创建了输出。只是在 union 语句输出中,首先出现的相应匹配列的别名显示在输出中。请看以下声明:
select sysdate as a1 from dual
union all
select sysdate as a2 from dual;
请注意,在输出中,列名是 a1(尽管您在第二个 select 语句中将其别名为 a2)。
另请注意,日期显示在结果集中的两个不同行中(即使 sysdate 的值相同)。如果您使用 UNION 而不是 UNION all,则会过滤所有重复的列。
这比 NoDisplayName 的解决方案笨拙得多,我只是在演示一种替代方法。通过在每个联合部分中为聚合函数添加一个 "dummy" 列,您可以获得每个部分的不同平均列。我将汇总移动到外层:
select D1, Day_of_Week, max(Infl_AHT), max(Non_inf_AHT)
from (
SELECT DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
AVG (T1.LengthSec + T1.Sec) AS Infl_AHT,
-- add dummy column
null as Non_inf_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.ID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=1
GROUP BY DATEPART(dw,t1.Date), DATENAME(dw,t1.Date)
Union ALL
select DATEPART(dw,t1.Date) AS D1,
DATENAME(dw,t1.Date) AS Day_of_Week,
-- add dummy column
null as Infl_AHT,
AVG (T1.LengthSec + T1.Sec) AS Non_inf_AHT
from bm.t1agent t1 with (nolock)
JOIN prc.Route rreq
on t1.id = rreq.t1CallID
join PRC.Routing rraw with (nolock)
on rreq.ID = rraw.Id
where t1.Date >= '2014-12-07'
AND rreq.Influenced=0
GROUP BY DATEPART(dw,t1.Date), DATENAME(dw,t1.Date)
)
GROUP BY ROLLUP (D1, Day_of_Week)
ORDER BY D1;