where部分的不同查询,如何转化为1个查询?
我有两个简单的查询,
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 非常基本..我有这个限制 :(
驱动程序的名称是 Relational DataBridge for RM Cobol
I have two simple queries
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'
the difference is the where, for example the first query results in 769686 and the second in 3469, what I have to do, to see the result of the two queries in one result?
result1 result2
----------------
769686 3469
I tried
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'
but this is not what I want because it results in 2 rows
result1
result2
I must say that I used this query under an ODBC bridge to connect Cobol files, the driver is tooo old.. so the sql is quite basic.. I have this limitation :(
the name of the driver is Relational DataBridge for RM Cobol
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
...得到:
Use:
...to get:
我想是这样的...
union 将查询置于彼此之上。
连接将查询放在一起。
在这种情况下我所做的是这样的...
为了简化这里发生的事情,假设 query1 是一个表,query2 是一个表
希望这会有所帮助。
I think of it like this...
union puts queries on top of each other.
joins put queries next to each other.
What I do in this situation is this...
To simplify what's going on here, just pretend that query1 is a table, and query2 is a table
Hope this helps.
尝试自加入。它会是这样的:
选择 sum(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'
Try a self join. It would be something like this:
select sum(C.deb)-sum(C.cre) as Result1, sum(X.deb)-sum(X.cre) as Result2
from CXC C, CXC X
where C.id='22731999' and X.id='22731999' and .tipo='IM'
尝试一下
Give this a try