2014-10-19 68 views
0

下面給出了基於新,舊錶值的新列是我的數據如何使甲骨文

Name Country 
s  India 
d  Ind 
c  Afric 
r  Africa 
f  Ind 
v  India 

有在全國列很多錯誤的結構。下面是一個包含識別錯誤

Old Value New Value Source Column 
Ind India Avox Country 
Afric Africa Avox Country 

我需要下表包含國家

的正確值
Name Country New_Column 
s  India India 
d  Ind  India 
c  Afric Africa 
r  Africa Africa 
f  Ind  India 
v  India India 

下面給出的是我使用的命令表。這只是數據的快照。我的數據非常大

merge into L03_a_AVOX_DATA_test n using (
    SELECT old_value , new_value 
     FROM Transformation_Data_all where column_identifier='Country' and Source_identifier='Avox' 
) o ON (n.Country = o.old_value) 
WHEN MATCHED THEN 
    n.New_Column = o.new_value; 

回答

1

使用DECODE函數更新所有不正確的列值。或者,爲了使其更加明確和可讀,請使用CASE結構。

使用DECODE

DECODE(column, 'Ind', 'India', 'Afric', 'Africa') 

使用CASE

CASE 
    WHEN column = 'Ind' 
     THEN 'India' 
    WHEN column = 'Afric' 
     THEN 'Africa' 
END 

所以,你的更新語句會是什麼樣子,

UPDATE TABLE_NAME 
    SET COUNTRY_NAME = <the decode or case expression as explained above> 
WHERE COUNTRY_NAME IN(country names list); 
0

「簡單」 的版本:使用兩個更新:

update the_table 
    set country = 'India' 
where country = 'Ind'; 

update the_table 
    set country = 'Africa' 
where country = 'Afric; 

一個使用一個單獨的語句

update the_table 
    set country = case 
        when country = 'Afric' then 'Africa' 
        when country = 'Ind' then 'India' 
       end 
where country in ('Afric', 'Ind'); 
0

你的「問題」比較複雜一點並不實際擁有的一個問題是這樣的。也許你想問的是「當我試圖運行MERGE語句時,爲什麼會出現編譯錯誤?」如果是這樣,答案是你需要在MATCHED子句中包含UPDATE關鍵字。

merge into L03_a_AVOX_DATA_test n 
using (
    SELECT old_value , new_value 
     FROM Transformation_Data_all 
     where column_identifier='Country' 
    and Source_identifier='Avox' 
    ) o 
    ON (n.Country = o.old_value) 
WHEN MATCHED THEN 
    UPDATE 
    SET n.New_Column = o.new_value; 

如果您需要填充所有行的NEW_COLUMN,爲你的結果集建議你做,那麼你可以做到這一點與運行合併後簡單的更新。

update 03_a_AVOX_DATA_test n 
set n.New_Column = n.Country 
where n.New_Column is null;