我想要合并两个结果集,因为我想将它们作为数据集发送到我的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
答案 0 :(得分:3)
SELECT Sum(Teamsales) as TS,
(
SELECT Sum(ProdSales)
FROM DEF
) AS PS
FROM ABC
答案 1 :(得分:1)
也许是这样的?
select
sum(teamsales) as ts,
Sum(ProdSales) as ps
from
abc cross join
def
(Crossjoins通常是邪恶的...但你可以考虑使用更严格的一个。)
答案 2 :(得分: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
答案 3 :(得分:0)
由于这些都是单行,你可以只是交叉加入结果,给你一行:
SELECT SUM(Teamsales) as TS,
FROM abc
CROSS JOIN (SELECT SUMN(ProdSales) FROM def) t
答案 4 :(得分: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