3个表上的DB2完全外部联接

时间:2019-03-08 14:50:38

标签: sql db2

我不明白如何管理所有3个表之间的链接。附上我的示例:

SELECT DISTINCT A.sort_kz,
                A.type_sett_inst                          AS INSTRUCTION_TYPE,
                A.wrg_kurs                                AS
                SETT_MARKET_VALUE_CURR,
                Coalesce(num_cbm, 0)                      AS NUMBER_1,
                CAST (Round(gg_cbm_1, 0) AS DECIMAL(20))  AS SETT_VALUE_CURR,
                CAST (Round(gg_cbm_2, 0) AS DECIMAL (20)) AS SETT_VALUE_EUR,
                CAST(Round(nomi_cbm, 0) AS DECIMAL (20))  AS NOMI_CBM,
                Coalesce(num_com, 0)                      AS NUMBER_2,
                CAST(Round(gg_com_1, 0) AS DECIMAL(20))   AS SETT_VALUE_CURR_2,
                CAST(Round(gg_com_2, 0) AS DECIMAL (20))  AS SETT_VALUE_EUR_2,
                CAST(Round(nomi_com, 0) AS DECIMAL (20))  AS NOMI_COM,
                Coalesce(num_fop, 0)                      AS NUMBER_3,
                CAST(Round(gg_fop_2, 0) AS DECIMAL (20))  AS MARKET_VALUE_EUR,
                CAST(Round(nomi_fop, 0) AS DECIMAL (20))  AS NOMI_FOP,
                CASE
                  WHEN dev_kurs_ah = 999999 THEN 0
                  ELSE dev_kurs_ah
                END                                       AS EXCHANGE_RATES
FROM   t2 A
       FULL JOIN t2a B
              ON B.wrg_kurs = A.wrg_kurs
                 AND B.type_sett_inst = A.type_sett_inst
                 AND B.sort_kz = A.sort_kz
       FULL JOIN t2b C
              ON C.wrg_kurs = A.wrg_kurs
                 AND C.type_sett_inst = A.type_sett_inst
                 AND C.sort_kz = A.sort_kz  

A和B分别连接C和A,但是C和B呢?表B的结果不正确。

3 个答案:

答案 0 :(得分:0)

您可以使用COALESCE()

ON C.wrg_kurs = COALESCE(A.wrg_kurs, B.wrg_kurs) AND
   C.type_sett_inst = COALESCE(A.type_sett_inst, B.type_sett_inst) AND
   C.sort_kz = COALESCE(A.sort_kz, B.sort_kz) 

SELECT中,您可能还需要COALESCE()

COALESCE(A.sort_kz, B.sort_kz, C.sort_kz) as sort_kz

答案 1 :(得分:0)

类似这样的示例数据总是有帮助的...

drop table t2;
drop table t2a;
drop table t2b;

create table #t2 (wrg_kurs int, type_sett_inst int, sort_kz int, some_val int);
create table #t2a (wrg_kurs int, type_sett_inst int, sort_kz int, some_val int);
create table #t2b (wrg_kurs int, type_sett_inst int, sort_kz int, some_val int);

insert into #t2 values (1, 1, 1, 9), (1, 2, 1, 9), (1, 3, 1, 21);
insert into #t2a values (1, 2, 1, 3), (1, 3, 1, 23), (1, 4, 1, 49);
insert into #t2b values (2, 2, 1, 3), (1, 2, 1, 7), (1, 4, 1, 32), (1, 5, 1, 51);

with
t2_t2a_foj as
(
SELECT case when A.wrg_kurs is null then B.wrg_kurs else A.wrg_kurs end as wrg_kurs
      ,case when A.type_sett_inst is null then B.type_sett_inst else A.type_sett_inst end as type_sett_inst
      ,case when A.sort_kz is null then B.sort_kz else A.sort_kz end as sort_kz
      ,isnull(a.some_val, 0) + isnull(b.some_val, 0) as some_val
      ,A.some_val as t2_some_val
      ,B.some_val as t2a_some_val
  FROM #t2 A
       FULL JOIN 
       #t2a B
          ON B.wrg_kurs = A.wrg_kurs
             AND B.type_sett_inst = A.type_sett_inst
             AND B.sort_kz = A.sort_kz
)
SELECT case when A.wrg_kurs is null then B.wrg_kurs else A.wrg_kurs end as wrg_kurs
      ,case when A.type_sett_inst is null then B.type_sett_inst else A.type_sett_inst end as type_sett_inst
      ,case when A.sort_kz is null then B.sort_kz else A.sort_kz end as sort_kz
      ,isnull(A.some_val, 0) + isnull(B.some_val, 0) as some_val
      ,A.t2_some_val as t2_some_val
      ,A.t2a_some_val as t2_some_val
      ,B.some_val as t2a_some_val
  FROM t2_t2a_foj A
       FULL JOIN 
       #t2b B
          ON A.wrg_kurs = B.wrg_kurs
             AND A.type_sett_inst = B.type_sett_inst
             AND A.sort_kz = B.sort_kz

答案 2 :(得分:0)

明白了。由公共表表达式中的预选择引起的错误。还是谢谢你

马库斯