where部分的不同查询,如何转化为1个查询?

发布于 2024-09-28 18:19:50 字数 736 浏览 3 评论 0原文

我有两个简单的查询,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必在意 2024-10-05 18:19:50

使用:

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

Use:

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' 

...to get:

result1  result2
----------------
769686   3469
本王不退位尔等都是臣 2024-10-05 18:19:50

我想是这样的...
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

希望这会有所帮助。

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...

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

To simplify what's going on here, just pretend that query1 is a table, and query2 is a table

select result1, result2 
from query1
join query2 on query1.id = query2.id

Hope this helps.

机场等船 2024-10-05 18:19:50

尝试自加入。它会是这样的:

选择 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'

别挽留 2024-10-05 18:19:50

尝试一下

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

Give this a try

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文