如果1列为空,则Oracle Join不返回数据

时间:2019-04-05 10:38:00

标签: oracle

我有Oracle查询来检索每个客户的产品价格,但是仅在为每个产品设置客户价格时才返回行(1、4、6、189、191、7、80、235)。

如何做到这一点,即使1列为空,它也能返回行?我想为每个客户排位,不管他们是否有产品价格。

我还尝试了价格表上的完全外部联接,左侧外部联接,但对结果没有影响。

举例说明我想要的:

900001,Some Customer,1.59,2.49,3.39,0,0,0,0,4.92

select
    ltrim(kust.kunr),
    kust_adr.ku_name,
    p1.sp_auf_m2,
    p2.sp_auf_m2,
    p3.sp_auf_m2,
    p4.sp_auf_m2,
    p5.sp_auf_m2,
    p6.sp_auf_m2,
    p7.sp_auf_m2,
    p8.sp_auf_m2
from
    kust
    inner join
        kust_adr
        on kust.kunr = kust_adr.ku_nr
    full outer join
        sp_przu p1
        on kust.kunr = p1.kunr
    full outer join
        sp_przu p2
        on kust.kunr = p2.kunr
    full outer join
        sp_przu p3
        on kust.kunr = p3.kunr
    full outer join
        sp_przu p4
        on kust.kunr = p4.kunr
    full outer join
        sp_przu p5
        on kust.kunr = p5.kunr
    full outer join
        sp_przu p6
        on kust.kunr = p6.kunr
    full outer join
        sp_przu p7
        on kust.kunr = p7.kunr
    full outer join
        sp_przu p8
        on kust.kunr = p8.kunr
where
    kust_adr.ku_adr_art = 0
    and p1.prl_nr = 2
    and p1.spr_nr = 1
    and p2.prl_nr = 2
    and p2.spr_nr = 4
    and p3.prl_nr = 2
    and p3.spr_nr = 6
    and p4.prl_nr = 2
    and p4.spr_nr = 189
    and p5.prl_nr = 2
    and p5.spr_nr = 191
    and p6.prl_nr = 2
    and p6.spr_nr = 7
    and p7.prl_nr = 2
    and p7.spr_nr = 80
    and p8.prl_nr = 2
    and p8.spr_nr = 235
order by
    kust.kunr;

1 个答案:

答案 0 :(得分:2)

您的and子句将外部联接转换为内部联接-将它们移入各自的on子句:

select
    ltrim(kust.kunr),
    kust_adr.ku_name,
    p1.sp_auf_m2,
    p2.sp_auf_m2,
    p3.sp_auf_m2,
    p4.sp_auf_m2,
    p5.sp_auf_m2,
    p6.sp_auf_m2,
    p7.sp_auf_m2,
    p8.sp_auf_m2
from
    kust
    inner join
        kust_adr
        on kust.kunr = kust_adr.ku_nr
    full outer join
        sp_przu p1
        on kust.kunr = p1.kunr
        and p1.prl_nr = 2
        and p1.spr_nr = 1
    full outer join
        sp_przu p2
        on kust.kunr = p2.kunr
        and p2.prl_nr = 2
        and p2.spr_nr = 4
    full outer join
        sp_przu p3
        on kust.kunr = p3.kunr
        and p3.prl_nr = 2
        and p3.spr_nr = 6
    full outer join
        sp_przu p4
        on kust.kunr = p4.kunr
        and p4.prl_nr = 2
        and p4.spr_nr = 189
    full outer join
        sp_przu p5
        on kust.kunr = p5.kunr
        and p5.prl_nr = 2
        and p5.spr_nr = 191
    full outer join
        sp_przu p6
        on kust.kunr = p6.kunr
        and p6.prl_nr = 2
        and p6.spr_nr = 7
    full outer join
        sp_przu p7
        on kust.kunr = p7.kunr
        and p7.prl_nr = 2
        and p7.spr_nr = 80
    full outer join
        sp_przu p8
        on kust.kunr = p8.kunr
        and p8.prl_nr = 2
        and p8.spr_nr = 235
where
    kust_adr.ku_adr_art = 0
order by
    kust.kunr;

我不确定他们是否真的需要完全外部联接;并且根据值对使用单个外部联接,然后旋转该结果可能会更简单,例如:

select *
from (
    select
        ltrim(k.kunr) as kunr,
        ka.ku_name,
        p.spr_nr,
        p.sp_auf_m2
    from
        kust k
        inner join
            kust_adr ka
            on k.kunr = ka.ku_nr
        left outer join
            sp_przu p
            on k.kunr = p.kunr
            and p.prl_nr = 2
            and p.spr_nr in (1, 4, 6, 189, 191, 7, 80, 235)
    where
        ka.ku_adr_art = 0
)
pivot (
    max(sp_auf_m2) for (spr_nr) in (1, 4, 6, 189, 191, 7, 80, 235)
)
order by
    kunr;

尽管您可能希望设置别名,而不是使默认的带引号的列名与spr_nr值匹配。

  

ORA-00933:SQL命令未正确结束...这是10g

pivot子句直到11g才被添加,但是您可以使用聚合的case表达式复制它的功能(实际上,它实际上是在后台执行的):

select
    ltrim(k.kunr) as kunr,
    ka.ku_name,
    max(case when p.spr_nr = 1 then p.sp_auf_m2 end) as sp_auf_m2_1,
    max(case when p.spr_nr = 4 then p.sp_auf_m2 end) as sp_auf_m2_4,
    max(case when p.spr_nr = 6 then p.sp_auf_m2 end) as sp_auf_m2_6,
    max(case when p.spr_nr = 189 then p.sp_auf_m2 end) as sp_auf_m2_189,
    max(case when p.spr_nr = 191 then p.sp_auf_m2 end) as sp_auf_m2_191,
    max(case when p.spr_nr = 7 then p.sp_auf_m2 end) as sp_auf_m2_7,
    max(case when p.spr_nr = 80 then p.sp_auf_m2 end) as sp_auf_m2_80,
    max(case when p.spr_nr = 235 then p.sp_auf_m2 end) as sp_auf_m2_235
from
    kust k
    inner join
        kust_adr ka
        on k.kunr = ka.ku_nr
    left outer join
        sp_przu p
        on k.kunr = p.kunr
        and p.prl_nr = 2
        and p.spr_nr in (1, 4, 6, 189, 191, 7, 80, 235)
where
    ka.ku_adr_art = 0
group by
    ltrim(k.kunr),
    ka.ku_name
order by
    kunr;