与MINUS和NOT IN子句相同的查询返回不同的结果集。请解释为什么这个不同的结果集

时间:2014-10-06 11:09:13

标签: sql oracle oracle11g

这里我使用MINUS进行相同的查询,使用NOT IN进行两次查询返回不同的结果集。

使用NOT IN子句查询

SELECT DISTINCT ebdf.business, ebdf.data_source, ebdf.frequency,
                          c.case_symbol
                     FROM etl_mgr.etl_bus_datasrc_frequencies@etlmgr_srv_ubatchh_lx ebdf INNER JOIN etl_mgr.etl_rulesets@etlmgr_srv_ubatchh_lx er
                          ON (    ebdf.business = er.business
                              AND ebdf.data_source = er.data_source
                              AND ebdf.frequency = er.frequency
                             )
                          LEFT OUTER JOIN etl_mgr.etl_db_output_fact_rules@etlmgr_srv_ubatchh_lx r
                          ON er.ruleset_id = r.ruleset_id
                          LEFT OUTER JOIN etl_mgr.etl_db_output_fact_cases@etlmgr_srv_ubatchh_lx c
                          ON r.db_output_fact_rule_id =
                                                      c.db_output_fact_rule_id
                    WHERE (ebdf.business,
                           ebdf.data_source,
                           ebdf.frequency,
                           c.case_symbol
                          ) NOT IN (
                             SELECT c.business_unit_key, e.data_source_key,
                                    f.data_frequency_key,
                                    g.data_source_subset
                               FROM npdreportmanager_appsetup.tb_module_summary_schema a INNER JOIN npdreportmanager_appsetup.tb_module b
                                    ON a.module = b.module
                                    INNER JOIN npdreportmanager_appsetup.tb_business_unit c
                                    ON b.business_unit_id = c.business_unit_id
                                    INNER JOIN npdreportmanager_appsetup.tb_summary_source_type d
                                    ON a.summary_source_type_id =
                                                      d.summary_source_type_id
                                    INNER JOIN npdreportmanager_appsetup.tb_data_source e
                                    ON d.data_source_id = e.data_source_id
                                    INNER JOIN npdreportmanager_appsetup.tb_data_frequency f
                                    ON d.data_frequency_id =
                                                           f.data_frequency_id
                                    LEFT OUTER JOIN npdreportmanager_appsetup.tb_data_source_subset g
                                    ON d.data_source_subset_id =
                                                       g.data_source_subset_id)

相同的查询与MINUS困惑为什么不返回相同的输出:

SELECT DISTINCT ebdf.business, ebdf.data_source, ebdf.frequency,
                          c.case_symbol
                     FROM etl_mgr.etl_bus_datasrc_frequencies@etlmgr_srv_ubatchh_lx ebdf INNER JOIN etl_mgr.etl_rulesets@etlmgr_srv_ubatchh_lx er
                          ON (    ebdf.business = er.business
                              AND ebdf.data_source = er.data_source
                              AND ebdf.frequency = er.frequency
                             )
                          LEFT OUTER JOIN etl_mgr.etl_db_output_fact_rules@etlmgr_srv_ubatchh_lx r
                          ON er.ruleset_id = r.ruleset_id
                          LEFT OUTER JOIN etl_mgr.etl_db_output_fact_cases@etlmgr_srv_ubatchh_lx c
                          ON r.db_output_fact_rule_id =
                                                      c.db_output_fact_rule_id
          MINUS
          SELECT c.business_unit_key, e.data_source_key, f.data_frequency_key,
                 g.data_source_subset
            FROM npdreportmanager_appsetup.tb_module_summary_schema a INNER JOIN npdreportmanager_appsetup.tb_module b
                 ON a.module = b.module
                 INNER JOIN npdreportmanager_appsetup.tb_business_unit c
                 ON b.business_unit_id = c.business_unit_id
                 INNER JOIN npdreportmanager_appsetup.tb_summary_source_type d
                 ON a.summary_source_type_id = d.summary_source_type_id
                 INNER JOIN npdreportmanager_appsetup.tb_data_source e
                 ON d.data_source_id = e.data_source_id
                 INNER JOIN npdreportmanager_appsetup.tb_data_frequency f
                 ON d.data_frequency_id = f.data_frequency_id
                 LEFT OUTER JOIN npdreportmanager_appsetup.tb_data_source_subset g
                 ON d.data_source_subset_id = g.data_source_subset_id

2 个答案:

答案 0 :(得分:0)

NOT NULL对空值敏感,它不比较空值因此忽略它。相反,MINUS对空值不敏感。检查下面给出的例子

SQL> create table test1 (c1 number(9), c2 varchar2(10));

Table created.

SQL> insert all
  2  into test1 values (1, 'XYZ')
  3  into test1 values (2, null)
  4  into test1 values (4, null)
  5  into test1 values (null, 'ABC')
  6  select * from dual;

4 rows created.

SQL> create table test2 (c1 number(9), c2 varchar2(10));

Table created.

SQL> insert all
  2  into test2 values (1, 'XYZ')
  3  into test2 values (2, null)
  4  into test2 values (3, 'GHI')
  5  into test2 values (null, 'ABC')
  6  into test2 values (5, null)
  7  select * from dual;

5 rows created.

SQL> select * from test2 where (c1, c2) not in (select c1, c2 from test1);

        C1 C2
---------- ----------
         3 GHI

SQL> select * from test2 minus select * from test1;

        C1 C2
---------- ----------
         3 GHI
         5

SQL>

5, NULLNOT NULLMINUS忽略,但被2, null考虑。实际上NOT NULL忽略了行null, ABCselect * from test2 where (c1, c2) in (select c1, c2 from test1);,但由于它们也存在于test1中,所以它似乎是合乎逻辑的,尽管被忽略了。这可以通过发布{{1}}

进行测试

答案 1 :(得分:0)

我修改了查询,并且这次能够得到正确的答案。感谢大家的迅速回应。

SELECT * FROM( SELECT DISTINCT ebdf.business || ebdf.data_source || ebdf.frequency ||c.case_symbol As D1,ebdf.business, ebdf.data_source, ebdf.frequency,
                          c.case_symbol
                     FROM etl_mgr.etl_bus_datasrc_frequencies@etlmgr_srv_ubatchh_lx ebdf INNER JOIN etl_mgr.etl_rulesets@etlmgr_srv_ubatchh_lx er
                          ON (    ebdf.business = er.business
                              AND ebdf.data_source = er.data_source
                              AND ebdf.frequency = er.frequency
                             )
                          LEFT OUTER JOIN etl_mgr.etl_db_output_fact_rules@etlmgr_srv_ubatchh_lx r
                          ON er.ruleset_id = r.ruleset_id
                          LEFT OUTER JOIN etl_mgr.etl_db_output_fact_cases@etlmgr_srv_ubatchh_lx c
                          ON r.db_output_fact_rule_id =
                                                      c.db_output_fact_rule_id) tst WHERE tst.D1 NOT IN
          (
          SELECT  c.business_unit_key || e.data_source_key || f.data_frequency_key ||  g.data_source_subset as D1

            FROM npdreportmanager_appsetup.tb_module_summary_schema a INNER JOIN npdreportmanager_appsetup.tb_module b
                 ON a.module = b.module
                 INNER JOIN npdreportmanager_appsetup.tb_business_unit c
                 ON b.business_unit_id = c.business_unit_id
                 INNER JOIN npdreportmanager_appsetup.tb_summary_source_type d
                 ON a.summary_source_type_id = d.summary_source_type_id
                 INNER JOIN npdreportmanager_appsetup.tb_data_source e
                 ON d.data_source_id = e.data_source_id
                 INNER JOIN npdreportmanager_appsetup.tb_data_frequency f
                 ON d.data_frequency_id = f.data_frequency_id
                 LEFT OUTER JOIN npdreportmanager_appsetup.tb_data_source_subset g
                 ON d.data_source_subset_id = g.data_source_subset_id