在一个查询中获取两个查询的结果

时间:2017-01-23 10:36:09

标签: sql tsql

我有两个问题

QUERY 1

SELECT SUM(amount) as m1 , bank
from test t1
group by bank 

QUERY 2

SELECT SUM(amount21) as m , bank
from test2 t1
group by bank

如何在一个查询中获取两个查询的结果?

m | m1 | bank

2 个答案:

答案 0 :(得分:3)

我想你想要FULL OUTER JOIN

select q1.m1, q2.m, coalesce(q1.bank, q2.bank)
from
(
    SELECT SUM(amount) as m1 , bank
    from test t1
    group by bank
) q1
FULL OUTER JOIN
(
    SELECT SUM(amount21) as m , bank
    from test2 t1
    group by bank
) q2 on q1.bank = q2.bank

答案 1 :(得分:0)

填写您需要的内容:

SELECT x.a, y.b 
FROM (SELECT * from a) as x, (SELECT * FROM b) as y
GROUP BY bank