我有两个简单的查询
select sum(deb)-sum(cre) as result1 from CXC where id='22731999'
select sum(deb)-sum(cre) as result2 from CXC where id='22731999' and tipo='IM'
区别在于,例如,第一个查询在769686中产生,第二个在3469中,我需要做什么,在一个结果中查看两个查询的结果?
result1 result2
----------------
769686 3469
我试过
select sum(C.deb)-sum(C.cre) as Result1 from CXC C where C.id='22731999'
UNION
select sum(X.deb)-sum(X.cre) as Result2 from CXC X where X.id='22731999' and .tipo='IM'
但这不是我想要的,因为它会导致2行
result1
result2
我必须说我在ODBC桥下使用此查询来连接Cobol文件,驱动程序太旧了..所以sql非常基本..我有这个限制:(
驱动程序的名称是RM Cobol的Relational DataBridge
答案 0 :(得分:1)
使用:
SELECT SUM(t.deb) - SUM(t.cre) as result1,
(SELECT SUM(x.deb) - SUM(x.cre)
FROM CXC x
WHERE x.id = t.id
AND x.tipo = 'IM') AS result2
FROM CXC t
WHERE t.id = '22731999'
......得到:
result1 result2
----------------
769686 3469
答案 1 :(得分:1)
我想起来就像这样...... union将查询置于彼此之上。 连接将查询放在一起。
在这种情况下我做的是......
SELECT result1, result2 FROM
(select sum(deb)-sum(cre) as result1, id from CXC where id='22731999' ) query1
JOIN
(select sum(deb)-sum(cre) as result2, id from CXC where id='22731999' and tipo='IM') query2
ON query1.id = query2.id
为了简化这里发生的事情,只需假装query1是一个表,而query2就是一个表
select result1, result2
from query1
join query2 on query1.id = query2.id
希望这有帮助。
答案 2 :(得分:0)
尝试自我加入。它会是这样的:
选择总和(C.deb)-sum(C.cre)作为Result1,sum(X.deb)-sum(X.cre)作为Result2
来自CXC C,CXC X
其中C.id ='22731999'和X.id ='22731999'和.tipo ='IM'
答案 3 :(得分:0)
试一试
SELECT max(Result1) as Result1, max(Result2) as Result2
FROM
(
SELECT
select sum(C.deb)-sum(C.cre) as Result1, null as Result2
from CXC C where C.id='22731999'
UNION
select null as Result1, sum(X.deb)-sum(X.cre) as Result2
from CXC X where X.id='22731999' and .tipo='IM'
) x