带条件的oracle sql合并语句

mw3dktmi  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(453)

我对sql比较陌生,我遇到了一个目标表没有更新的问题。我有重复的帐户#(键),在相关列中有不同的联系信息。我正在尝试将联系人信息(源)合并到一个行/帐号中,而不重复的联系人信息将合并到(目标)扩展列中。

我构造了一个带有case条件的merge语句来检查目标表中是否存在数据。如果数据不在目标表中,则在扩展列中添加信息。问题是目标表没有得到更新。源表和目标表都定义了相似性。


**Merge SQL- reduced query**

MERGE INTO target tgt
    USING (select accountno, cell, site, contact, email1 from (select w.accountno, w.cell, w.site, w.contact, email1, row_number() over (PARTITION BY w.accountno order by accountno desc) acct 
    from source w) inn where inn.acct =1) src 
    ON (tgt.accountno = src.accountno)
WHEN MATCHED
    THEN 
        UPDATE SET
                  tgt.phone4 =  
                  CASE WHEN src.cell <> tgt.cell
                  THEN src.cell
                  END,
                  tgt.phone5 =
                  CASE WHEN src.site <> tgt.site
                  THEN src.site
                  END

我已经确认源表中有accountno的联系信息,应该添加到目标表中。我非常感谢任何关于为什么不更新目标表的见解。我在堆栈上看到了一个类似的问题,但没有得到回答。

nukf8bse

nukf8bse1#

using子句中的src子查询只为每个accountno返回1个随机行。您需要聚合它们,例如使用pivot:

with source(accountno, cell, site, contact) as ( --test data:
   select 1,8881234567,8881235678,8881236789 from dual union all
   select 1,8881234567,8881235678,8881236789 from dual
)
select accountno, contact, 
       r1_cell, r1_site, 
       r2_cell, r2_site
from (select s.*,row_number()over(partition by accountno order by cell) rn
      from source s
     )
pivot (
   max(cell) cell,max(site) site
    FOR rn
    IN (1 R1,2 R2)
)

最后,您可以将r1\u cell、r1\u site、r2\u cell、r2\u site与目标值进行比较,并使用所需的值:

MERGE INTO target tgt
    USING (
            select accountno, contact, 
                   r1_cell, r1_site, 
                   r2_cell, r2_site
            from (select s.*,row_number()over(partition by accountno order by cell) rn
                  from source s
                 )
            pivot (
               max(cell) cell,max(site) site
                FOR rn
                IN (1 R1,2 R2)
            )
          ) src 
    ON (tgt.accountno = src.accountno)
WHEN MATCHED
    THEN 
        UPDATE SET
                  tgt.phone4 =  
                  CASE 
                       WHEN src.r1_cell <> tgt.cell 
                          THEN src.r1_cell
                          ELSE src.r2_cell
                  END,
                  tgt.phone5 =
                  CASE WHEN src.r1_site <> tgt.site 
                     THEN src.r1_site
                     ELSE src.r2_site
                  END
/
whhtz7ly

whhtz7ly2#

问题在于您在第\行中使用的逻辑,即用相同的帐号对行进行编号。

MERGE 
 INTO target tgt
USING (select accountno, cell, site, contact, email1 
         from (select w.accountno, w.cell, w.site, w.contact, email1
                    , row_number() over (PARTITION BY w.accountno order by w.accountno desc) acct 
                from source w
              left join target w2
                  on w.accountno=w2.accountno
               where w2.cell is null /* get records which are not in target*/ 
              ) inn 
         where inn.acct =1
       ) src 
   ON (tgt.accountno = src.accountno)
WHEN MATCHED THEN 
     UPDATE 
        SET tgt.phone4 = src.cell,
            tgt.phone5 = src.site

相关问题