tags:

views:

65

answers:

1

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??

+2  A: 

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:

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>

We can use an analytic ROW_NUMBER() to generate the necessary handle for 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>

And this is the desired result (barring any further additions to the business rules):

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>
APC
you are using decode (department_code,'01','Comp_1', '02', 'Comp_2','03','Comp_3'). But in my case i dont know what values will be there insteat of '01','02','03'. It could be anything. only thing is that there will be three entries for each item
Jasim
@Jasim - if you want us to help, you really ought to give us *all* the relevant information upfront.
APC