【问题标题】:SQL Left Outer Join not returning all rows from left table (no where clause filter)SQL Left Outer Join 未返回左表中的所有行(无 where 子句过滤器)
【发布时间】:2021-10-04 08:33:00
【问题描述】:

我有一个左外连接,它不会返回“左”表中的所有行。我没有 where 子句,所以在 join 之后不应该应用过滤。

我期待:

Product 1 AT 100
Product 2 AT 25
Product 4 AT 57
Product 1 GR 45
Product 2 GR 22
Product 3 GR 5
Product 4 GR 4
Product 3 null null

但我错过了最后一行。非常感谢您对此提出的任何启示。

复制它:

-- Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

drop table t1;
drop table t2;


create table t1 
(ov_product          varchar2(18 byte)
,product             varchar2(18 byte)
)
/

create table t2 
(reporting_month     number
,product             varchar2(18 byte) 
,sender              varchar2(2 byte) 
,items               number 
)
/

insert into t1
(
select 'Product 1' ov_product, 'P1' product from dual
union
select 'Product 2' ov_product, 'P2' product from dual
union
select 'Product 3' ov_product, 'P3' product from dual
union
select 'Product 4' ov_product, 'P4' product from dual
);

insert into t2
(
select 202108, 'P1', 'AT', 100 from dual
union
select 202108, 'P2', 'AT', 25 from dual
union
-- no P3 for AT
select 202108, 'P4', 'AT', 57 from dual
union
select 202108, 'P1', 'GR', 45 from dual
union
select 202108, 'P2', 'GR', 22 from dual
union
select 202108, 'P3', 'GR', 5 from dual
union
select 202108, 'P4', 'GR', 4 from dual
)
;
commit;
select t1.ov_product
      ,t2.sender
      ,t2.items
  from t1
  left outer join t2
    on t1.product = t2.product
 order by 2, 1   
;

【问题讨论】:

    标签: sql oracle left-join


    【解决方案1】:

    您的外部连接工作正常。

    您可能是指分区外连接。

    查看加入中的额外query_partition_clause

    PARTITION BY (sender) 只有这个连接才能填补sender 中的空白,如您所愿。

    select t1.ov_product
          ,t2.sender
          ,t2.items
      from t1
      left outer join t2
      PARTITION BY  (sender)
        on t1.product = t2.product   
     order by 2, 1
    
    
    OV_PRODUCT         SE      ITEMS
    ------------------ -- ----------
    Product 1          AT        100
    Product 2          AT         25
    Product 3          AT           
    Product 4          AT         57
    Product 1          GR         45
    Product 2          GR         22
    Product 3          GR          5
    Product 4          GR          4
    

    【讨论】:

      【解决方案2】:

      由于在 t2 中有一行与产品 3,因此左连接不会产生空值记录。

      为了获得这样的线路,您需要加入一个发件人表(如果有的话)。或者,如果该表不存在,您可以使用这样的 CTE

      with senders(sender) as (select distinct sender from t2)

      with senders(sender) as (select distinct sender from t2)
      
      select t1.ov_product
            ,t2.sender
            ,t2.items
        from t1
        cross join senders
        left outer join t2
          on t1.product = t2.product
         and t2.sender = senders.sender
       order by 2, 1;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-30
        • 2010-11-26
        • 2016-11-06
        • 1970-01-01
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多