聚合函数在Oracle 10g中无法正常运行

时间:2016-08-18 09:53:19

标签: sql join oracle10g aggregate-functions

我使用 Oracle 10g 版本

select * from v$version

结果:

Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0  Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

有查询#1 。此查询正常工作并产生预期结果。

SELECT   ai.file_id,
         fi.tr_file,
         bi.recv_file
FROM (SELECT fe.file_id FROM file_journal fe WHERE fe.out_ = 0) ai,
         (SELECT DISTINCT recv_file FROM send_docs sd WHERE sd.recv_file IS NOT NULL) bi,
         (SELECT DISTINCT f1.file_id tr_file
                FROM send_treasure s,
                         payment_docs  p1,
                         payment_docs  p2,
                         file_journal  f1,
                         file_journal  f3,
                         file_journal  f4,
                         r_file_states t1,
                         r_file_states t2
             WHERE s.sid = p1.sid
                 AND p1.recv_file = f1.file_id
                 AND s.send_file = p2.recv_file
                 AND p2.recv_file = f3.file_id
                 AND f3.state = t1.code
                 AND f4.state = t2.code
                 AND p2.send_file_b = f4.file_id) fi
WHERE ai.file_id = bi.recv_file
 AND ai.file_id = fi.tr_file

此查询已简化,保留了加入所需的列。

此查询返回以下预期结果:

    file_id     tr_file     recv_file
-------------------------------------- 
1   1623156799  1623156799  1623156799
2   1623156812  1623156812  1623156812
3   1623156813  1623156813  1623156813
4   1623156814  1623156814  1623156814
5   1623156815  1623156815  1623156815
6   1623156816  1623156816  1623156816
7   ....
and so on, a total of 18 entries.

然后,我需要计算记录数。为此,我执行以下查询#2

SELECT COUNT(*) cnt
FROM (SELECT ai.file_id,
             fi.tr_file,
             bi.recv_file
            FROM (SELECT fe.file_id FROM file_journal fe WHERE fe.out_ = 0) ai,
                 (SELECT DISTINCT recv_file FROM send_docs sd WHERE sd.recv_file IS NOT NULL) bi,
                 (SELECT DISTINCT f1.file_id tr_file
                                FROM send_treasure s,
                                     payment_docs  p1,
                                     payment_docs  p2,
                                     file_journal  f1,
                                     file_journal  f3,
                                     file_journal  f4,
                                     r_file_states t1,
                                     r_file_states t2
                             WHERE s.sid = p1.sid
                                 AND p1.recv_file = f1.file_id
                                 AND s.send_file = p2.recv_file
                                 AND p2.recv_file = f3.file_id
                                 AND f3.state = t1.code
                                 AND f4.state = t2.code
                                 AND p2.send_file_b = f4.file_id) fi
             WHERE ai.file_id = bi.recv_file
                 AND ai.file_id = fi.tr_file)

此查询返回以下预期结果:

CNT
---
18

然后我再次提出#3 查询。 此查询无法正常工作

SELECT *
FROM (SELECT COUNT(*) cnt
                FROM (SELECT ai.file_id,
                             fi.tr_file,
                             bi.recv_file
                            FROM (SELECT fe.file_id FROM file_journal fe WHERE fe.out_ = 0) ai,
                                 (SELECT DISTINCT recv_file FROM send_docs sd WHERE sd.recv_file IS NOT NULL) bi,
                                 (SELECT DISTINCT f1.file_id tr_file
                                                FROM send_treasure s,
                                                     payment_docs  p1,
                                                     payment_docs  p2,
                                                     file_journal  f1,
                                                     file_journal  f3,
                                                     file_journal  f4,
                                                     r_file_states t1,
                                                     r_file_states t2
                                             WHERE s.sid = p1.sid
                                                 AND p1.recv_file = f1.file_id
                                                 AND s.send_file = p2.recv_file
                                                 AND p2.recv_file = f3.file_id
                                                 AND f3.state = t1.code
                                                 AND f4.state = t2.code
                                                 AND p2.send_file_b = f4.file_id) fi
                             WHERE ai.file_id = bi.recv_file
                                 AND ai.file_id = fi.tr_file))

此查询返回结果:

CNT
---
0

如您所见,结果不正确

正确的结果应为:

CNT
---
18

P.S。 创建名为v_count_test视图。然后查询它,我们得到以下内容:

create or replace view v_count_test as
here query #2

select * from V_COUNT_TEST t

此查询返回结果:

CNT
---
0

我将非常感谢帮助和澄清情况。

非常感谢你!

演示视频错误运行查询。

Demonstration video incorrect running queries.

0 个答案:

没有答案