I have two tables like the below one in my DB. In the first table ITEM for evry DEPARTMENT_CODE there will be multiple ITEM_CODE.
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"
-------------------------------------------------------------
In the Table COMPETITOR evey ITEMCODE will have three entries and have different competitor_Code for that
I have three values Comp_1, comp_2, comp_3 and department_code =14;
what i want to do is update COMPETITOR table with comp_1, comp_2, comp_3 for evry Item_code which has department_code as 14 in ITEM table
sample output
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"
-------------------------------------------------------------
How can i write a single oracle query for this??
The following assumes that an ITEM_CODE is assigned to only one DEPARTMENT_CODE, because it’s simple and you haven’t given us any further business rules. It this assumption is wrong you will need to adjust the logic accordingly.
I have also incorporated your requirement that the existing value of COMPETITOT.COMPETITOR_CODE is unreliable.
Given this test date:
We can use an analytic ROW_NUMBER() to generate the necessary handle for COMPETITOR_CODE:
And this is the desired result (barring any further additions to the business rules):