我有两个要合并的结果集,因为我想将它们作为数据集发送到我的 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
我有两个要合并的结果集,因为我想将它们作为数据集发送到我的 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
SELECT Sum(Teamsales) as TS,
(
SELECT Sum(ProdSales)
FROM DEF
) AS PS
FROM ABC
很可能每个结果集中都有不止一行。正如您将在 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
也许是这样的?
select
sum(teamsales) as ts,
Sum(ProdSales) as ps
from
abc cross join
def
(交叉连接通常是邪恶的......但你可以考虑使用更严格的交叉连接。)
由于这些都是单行,你可以交叉加入结果,给你一行:
SELECT SUM(Teamsales) as TS,
FROM abc
CROSS JOIN (SELECT SUMN(ProdSales) FROM def) t
尝试这个:
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