在SQL中联合select语句

时间:2015-01-12 15:50:45

标签: sql sql-server union

我正在尝试联合我在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语句?

4 个答案:

答案 0 :(得分:4)

我认为您可以使用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) )) 

答案 1 :(得分:0)

UNION类似于垂直连接。它汇集了在其字段中具有相同内容的查询。

您要做的是水平连接两个具有不同字段的查询,这将通过D1或Day_of_of_Week字段上的标准JOIN来完成。

答案 2 :(得分:0)

最有可能正确创建输出。只是在union语句输出中,首先出现的相应匹配列的别名将显示在输出中。请参阅以下声明:

select sysdate as a1 from dual
union all
select sysdate as a2 from dual;

请注意,在输出中,列名是a1(尽管你为第二个select语句别名a2)。

另请注意,日期显示在结果集中的两个不同的行中(即使sysdate的值相同)。如果使用UNION而不是UNION all,则会过滤所有重复的列。

答案 3 :(得分:0)

这比NoDisplayName的解决方案更笨拙,我只是在演示另一种方法。通过在每个联合零件中为聚合函数添加“虚拟”列,可以为每个零件获得不同的平均列。我将汇总移动到外层:

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;