两个相同的查询给出不同的结果

发布于 2024-11-07 05:34:07 字数 1054 浏览 0 评论 0原文

我有 2 个查询:一个是用 ANSI SQL 编写的,另一个是使用 oracle 方言编写的。 我认为他们都必须给出相同的结果集,但事实并非如此。第一个查询给出 385 行,第二个查询仅给出 25 行

First:

SELECT  idclient, cl.surname, sum(sub1.s) 
    FROM client cl JOIN incomestatement incst USING(idclient)
                   JOIN (SELECT c.idincome ID, sum(inst.total) AS s  
                         FROM instalment inst JOIN credit c  USING(idcredit)
                         WHERE inst.paydate > c.paydate AND c.isloaned = 1
                         GROUP BY c.idincome) sub1 ON incst.idincome = sub1.ID
   GROUP BY idclient, cl.surname;

Second:

SELECT  c.idclient, c.surname, sum(sub.s)
FROM client c, incomestatement inc,
      (SELECT sum(inst.total) as s, cr.idincome as id
       FROM instalment inst, credit cr
       WHERE inst.paydate > cr.paydate AND cr.isloaned = 1 AND cr.idcredit = inst.idcredit
       GROUP BY cr.idincome
       ) sub
WHERE c.idclient = inc.idclient AND inc.income = sub.ID
group by c.idclient, c.surname;

那么为什么他们不给出相同的结果呢?

I have 2 queries: one is written in ANSI SQL, another is written using oracle dialect.
I think that they both must give the same resultset, but it is no true. First query gives 385 rows and the second - only 25

First:

SELECT  idclient, cl.surname, sum(sub1.s) 
    FROM client cl JOIN incomestatement incst USING(idclient)
                   JOIN (SELECT c.idincome ID, sum(inst.total) AS s  
                         FROM instalment inst JOIN credit c  USING(idcredit)
                         WHERE inst.paydate > c.paydate AND c.isloaned = 1
                         GROUP BY c.idincome) sub1 ON incst.idincome = sub1.ID
   GROUP BY idclient, cl.surname;

Second:

SELECT  c.idclient, c.surname, sum(sub.s)
FROM client c, incomestatement inc,
      (SELECT sum(inst.total) as s, cr.idincome as id
       FROM instalment inst, credit cr
       WHERE inst.paydate > cr.paydate AND cr.isloaned = 1 AND cr.idcredit = inst.idcredit
       GROUP BY cr.idincome
       ) sub
WHERE c.idclient = inc.idclient AND inc.income = sub.ID
group by c.idclient, c.surname;

So why they don't give the same result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

流绪微梦 2024-11-14 05:34:07

我会逐步解决这个问题。

  1. 两个子查询是否产生相同的数据集?
    • 如果是,请继续执行第 2 步。
    • 如果没有,那么您有两个更简单的查询需要分析和剖析。
  2. 鉴于这对子查询产生相同的答案,您可以确定 Client 和 IncomeStatement 连接是否给出相同的结果(将其视为另一个子查询)
    • 如果是,请继续执行第 3 步。
    • 如果没有,那么您需要分析和剖析一对查询(一个使用 JOIN,一个使用经典 SQL 表示法)。
  3. 假设这对连接和这对子查询均产生相同的结果,请分析为什么它们的连接无法正常工作。

I'd approach the problem in steps.

  1. Do the two sub-queries produce the same data sets?
    • If they do, proceed to step 2.
    • If not, then you have two simpler queries to analyze and dissect.
  2. Given that the pair of sub-queries produce the same answer, you can then establish whether the Client and IncomeStatement joins give the same results (treat it as another sub-query)
    • If they do, proceed to step 3.
    • If not, then you have a pair of queries (one with JOIN, one with classic SQL notation) to analyze and dissect.
  3. Given that the pair of joins and the pair of subqueries each produce the same result, analyze why the join of these does not work correctly.
北城挽邺 2024-11-14 05:34:07

你做出承诺了吗?
您可能没有提交某些事务,因此结果可能会有所不同。

Have you made a commit?
It's possible that you don´t commited some transactions, so the results can be differents.

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