我有一个包含这些列的表:
所有列都是nvarchar类型。
当我运行这样的查询时:
select vendor,SUM(cast(SuccessCalls as int)) as successCalls,
SUM(cast(TotalCalls as int)) as TotalCalls,
SUM(cast(TotalMins as decimal(18,2))) as TotalMins,
case when SUM(cast(TotalCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(SuccessCalls as decimal(18,2))) / SUM(cast(TotalCalls as decimal(18,2)))) end as ASR,
case when SUM(cast(ACDSuccessCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(TotalMins as decimal(18,2))) / SUM(cast(ACDSuccessCalls as decimal(18,2)))) end as ACD,
SUM(cast(Profix as decimal(18,2))) as profix
from InterfaceMeraAdReport_CAV
where FromTime >= '20140717134000' and ToTime <= '20140717135000'
and Customer= '01.2136' and Area in ('62811','62812','62813','62821','62822','62823','62852','62853')
and Vendor='22394'
group by vendor
我得到了这样的结果:
如果我稍微更改了查询的WHERE条件:
select vendor,SUM(cast(SuccessCalls as int)) as successCalls,
SUM(cast(TotalCalls as int)) as TotalCalls,
SUM(cast(TotalMins as decimal(18,2))) as TotalMins,
case when SUM(cast(TotalCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(SuccessCalls as decimal(18,2))) / SUM(cast(TotalCalls as decimal(18,2)))) end as ASR,
case when SUM(cast(ACDSuccessCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(TotalMins as decimal(18,2))) / SUM(cast(ACDSuccessCalls as decimal(18,2)))) end as ACD,
SUM(cast(Profix as decimal(18,2))) as profix
from InterfaceMeraAdReport_CAV
where FromTime >= '20140717133000' and ToTime <= '20140717135000'
and Customer= '01.2136' and Area in ('62811','62812','62813','62821','62822','62823','62852','62853')
and Vendor='22394'
group by vendor
我会得到像这样的结果:
我不知道的是,我可以将两个查询合并到一个查询中并获得包含两个结果行的结果吗?
答案 0 :(得分:1)
只要所有列的命名和格式相同,您就可以始终使用UNION
将结果集连接在一起。
(
select vendor,SUM(cast(SuccessCalls as int)) as successCalls,
SUM(cast(TotalCalls as int)) as TotalCalls,
SUM(cast(TotalMins as decimal(18,2))) as TotalMins,
case when SUM(cast(TotalCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(SuccessCalls as decimal(18,2))) / SUM(cast(TotalCalls as decimal(18,2)))) end as ASR,
case when SUM(cast(ACDSuccessCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(TotalMins as decimal(18,2))) / SUM(cast(ACDSuccessCalls as decimal(18,2)))) end as ACD,
SUM(cast(Profix as decimal(18,2))) as profix
from InterfaceMeraAdReport_CAV
where FromTime >= '20140717134000' and ToTime <= '20140717135000'
and Customer= '01.2136' and Area in ('62811','62812','62813','62821','62822','62823','62852','62853')
and Vendor='22394'
group by vendor
)
UNION
(
select vendor,SUM(cast(SuccessCalls as int)) as successCalls,
SUM(cast(TotalCalls as int)) as TotalCalls,
SUM(cast(TotalMins as decimal(18,2))) as TotalMins,
case when SUM(cast(TotalCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(SuccessCalls as decimal(18,2))) / SUM(cast(TotalCalls as decimal(18,2)))) end as ASR,
case when SUM(cast(ACDSuccessCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(TotalMins as decimal(18,2))) / SUM(cast(ACDSuccessCalls as decimal(18,2)))) end as ACD,
SUM(cast(Profix as decimal(18,2))) as profix
from InterfaceMeraAdReport_CAV
where FromTime >= '20140717133000' and ToTime <= '20140717135000'
and Customer= '01.2136' and Area in ('62811','62812','62813','62821','62822','62823','62852','62853')
and Vendor='22394'
group by vendor
)
但是,您可能需要考虑添加一个额外的列来标识哪个结果集来自哪个查询。