SQL oracle - 删除重复记录并更新其他表中的记录

时间:2015-08-18 15:58:23

标签: sql oracle

要求 - 删除重复记录,例如从2个表中更新其他表中的记录。

输入

Table1   Dim_Ctry   

PK_Key1 Country 
100     Argentina   
200     ARGENTINA   
300     India   
400     INDIA   

Table2  Dim_Geo     

PK_Key2  Geo    
500      Globe  
600      GLOBE  
700      Market 
800      MARKET 
900      Unique 

Table   Fact1   

PK_Key15    FK_KEY1 FK_KEY2
1           100     500
2           200     600
3           300     800
4           400     900

Table   Fact2   

PK_Key16    FK_KEY1 FK_KEY2
5           100     500
6           200     600
7           200     700
8           300     800

输出

Table1   Dim_Ctry           

PK_Key1 Country     
100     Argentina                   
300     India               

Table2   Dim_Geo        

PK_Key2  Geo        
500      Globe              
700      Market     
900      Unique     

Table   Fact1       

PK_Key15    FK_KEY1 FK_KEY2 
1           100     500 
2           100     500 
3           300     800 
4           300     800 

Table   Fact2       

PK_Key16    FK_KEY1 FK_KEY2 comment
5           100     500 
6           100     500 
7           100     700 
7           300     800 
8           1000    2000    no record in dim table just retain

1 个答案:

答案 0 :(得分:0)

您需要执行几个步骤。

  • 步骤1更新相关表格

将所有FK_KEY更新为最小值。

UPDATE Fact1 f1
SET 
FK_KEY1 = (SELECT MIN(PK_Key1)
           FROM Dim_Ctry dc1
           WHERE UPPER(dc1.Country) = (SELECT UPPER(dc2.Country)
                                       FROM Dim_Ctry dc2
                                       WHERE dc2.PK_Key1 = f1.FK_KEY1)
          ),
FK_KEY2 = (SELECT MIN(PK_Key2)
           FROM Dim_Geo dg1
           WHERE UPPER(dg1.Geo) = (SELECT UPPER(dg2.Geo)
                                   FROM Dim_Geo dg2
                                   WHERE dg2.PK_Key2 = f1.FK_KEY2)
          );
  • 第2步删除重复的

这将删除所有重复的内容并保留ID较小的内容

DELETE FROM Dim_Ctry dc1
WHERE EXISTS (SELECT PK_Key1 
              FROM Dim_Ctry  dc2
              WHERE dc1.PK_Key1 > dc2.PK_Key1 
              and  UPPER(dc1.Country) = UPPER(dc2.Country)
  • 第3步更新文字

您应该更新为标准格式的更低或更高。

UPDATE Dim_Ctry
SET Country = UPPER(Country) 

调试查询

SELECT f1.PK_Key15, f1.FK_KEY1, f1.FK_KEY2, 
             (SELECT UPPER(dc2.Country) 
              FROM Dim_Ctry dc2 
              WHERE dc2.PK_Key1 = f1.FK_KEY1
              ) as CurrentName,
              (SELECT MIN(PK_Key1) 
               FROM Dim_Ctry dc1 
               WHERE UPPER(dc1.Country) = (SELECT UPPER(dc2.Country) FROM Dim_Ctry dc2 WHERE dc2.PK_Key1 = f1.FK_KEY1) ) as minKey
FROM Fact1 f1