1

我有两个要合并的结果集,因为我想将它们作为数据集发送到我的 ssrs 报告中。

我目前正在做:

Select Sum(Teamsales) as TS, Null as PS from ABC;
 union 
Select null as TS, Sum(ProdSales) From DEF;

我得到这样的输出:

   123    NULL
   NULL   456

有没有办法得到如下输出?

 123  456
4

5 回答 5

3
SELECT Sum(Teamsales) as TS, 
(
  SELECT Sum(ProdSales) 
  FROM DEF 
) AS PS
FROM ABC
于 2013-11-12T16:54:11.177 回答
1

很可能每个结果集中都有不止一行。正如您将在 group by 子句中应用维度一样。

例如

您想获得每个分支的总团队销售额和总产品销售额,那么您应该编写。

Select ABC.BranchID, ABC.TS, DEF.PS
(Select BranchID, Sum(Teamsales) as TS from ABC group by BranchID) ABC
Inner Join
(Select BranchID, Sum(ProdSales) as PS From DEF group by BranchID) DEF
On ABC.BranchID = DEF.BranchID
于 2013-11-12T17:20:37.957 回答
1

也许是这样的?

select
  sum(teamsales) as ts,
  Sum(ProdSales) as ps
from
  abc cross join
  def

(交叉连接通常是邪恶的......但你可以考虑使用更严格的交叉连接。)

于 2013-11-12T16:50:43.167 回答
0

由于这些都是单行,你可以交叉加入结果,给你一行:

SELECT SUM(Teamsales) as TS, 
FROM   abc
CROSS JOIN (SELECT SUMN(ProdSales) FROM def) t
于 2013-11-12T16:52:25.463 回答
0

尝试这个:

select sum(TS), sum(PS) from (
Select Sum(Teamsales) as TS, Null as PS from ABC
 union all
Select null as TS, Sum(ProdSales) as PS From DEF
) as x
于 2013-11-12T16:54:58.257 回答