2010-06-22 113 views
0

我在我的數據庫中有兩個像下面這樣的表。在evry DEPARTMENT_CODE的第一個ITEM項目中,會有多個ITEM_CODE。Oracle多重更新查詢

ITEM 
---------------------------------------------------- 
"STORE_CODE" "ITEM_CODE" "DEPARTMENT_CODE" 
"011"   "912003" "14" 
"011"   "912004" "14" 
"011"   "914001" "14" 
---------------------------------------------------- 

COMPETITOR 
-------------------------------------------------------------- 
"STORE_CODE" "ITEM_CODE" "DEPARTMENT_CODE" "COMPETITOR_CODE" 

"011"  "912003"  "14"  "01" 
"011"  "912003"  "14"  "02" 
"011"  "912003"  "14"  "03" 
"011"  "912004"  "14"  "01" 
"011"  "912004"  "14"  "02" 
"011"  "912004"  "14"  "04" 
"011"  "914001"  "14"  "01" 
"011"  "914001"  "14"  "02" 
"011"  "914001"  "14"  "03" 
------------------------------------------------------------- 

在表COMPETITOR埃維ITEMCODE將有三個條目,並具有該

不同competitor_Code我有三個值COMP_1,comp_2,comp_3和department_code = 14;

我想做的事是COMP_1,comp_2,comp_3對於埃夫裏ITEM_CODE這ITEM表已department_code 14個更新COMPETITOR表

樣本輸出

COMPETITOR 
-------------------------------------------------------------- 
"STORE_CODE" "ITEM_CODE" "DEPARTMENT_CODE" "COMPETITOR_CODE" 

"011"  "912003"  "14"  "Comp_1" 
"011"  "912003"  "14"  "Comp_2" 
"011"  "912003"  "14"  "Comp_3" 
"011"  "912004"  "14"  "Comp_1" 
"011"  "912004"  "14"  "Comp_2" 
"011"  "912004"  "14"  "Comp_3" 
"011"  "914001"  "14"  "Comp_1" 
"011"  "914001"  "14"  "Comp_2" 
"011"  "914001"  "14"  "Comp_3" 
------------------------------------------------------------- 

我怎麼能寫一個Oracle查詢這個?

回答

2

以下內容假定ITEM_CODE僅分配給一個DEPARTMENT_CODE,因爲它很簡單,並且您沒有給我們任何其他業務規則。這個假設是錯誤的,你需要相應地調整邏輯。

我還納入您的要求,COMPETITOT.COMPETITOR_CODE的現有值是不可靠的。

鑑於本次測試日期:

SQL> select * from competitor 
    2/

STORE_CODE ITEM_CODE DEPARTMENT_CODE COMPETITOR 
---------- ---------- --------------- ---------- 
     11  912003    14 01 
     11  912003    14 04 
     11  912003    14 03 
     11  912004    14 01 
     11  912004    14 02 
     11  912004    14 04 
     11  914001    14 01 
     11  914001    14 02 
     11  914001    14 05 

9 rows selected. 

SQL> 

我們可以用一個解析ROW_NUMBER()來生成COMPETITOR_CODE必要的手柄:

SQL> update competitor c 
    2 set competitor_code = 
    3   (select decode (dr 
    4       , 1, 'Comp_1' 
    5       , 2, 'Comp_2' 
    6       , 3, 'Comp_3') 
    7    from (select row_number() over (partition by x.item_code 
    8             order by x.rowid) as dr 
    9       , x.rowid as row_id 
10     from competitor x 
11     where x.item_code in (select item_code 
12           from item 
13           where department_code = 14)) l 
14    where c.rowid = l.rowid) 
15/

9 rows updated. 

SQL> 

這是期望的結果(除非出現進一步增加到商業規則):

SQL> select * from competitor 
    2/

STORE_CODE ITEM_CODE DEPARTMENT_CODE COMPETITOR 
---------- ---------- --------------- ---------- 
     11  912003    14 Comp_1 
     11  912003    14 Comp_2 
     11  912003    14 Comp_3 
     11  912004    14 Comp_1 
     11  912004    14 Comp_2 
     11  912004    14 Comp_3 
     11  914001    14 Comp_1 
     11  914001    14 Comp_2 
     11  914001    14 Comp_3 

9 rows selected. 

SQL> 
+0

您正在使用解碼(department_code,'01','Comp_1','02' ,'Comp_2','03','Comp_3')。但在我的情況下,我不知道'01','02','03'會帶來什麼樣的價值。它可能是任何東西。唯一的問題是每個項目會有三個條目 – Andromeda 2010-06-22 07:36:34

+3

@Jasim - 如果您希望我們提供幫助,您應該提前向我們提供所有相關信息。 – APC 2010-06-22 09:56:34