Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:20:53+00:00 2026-05-15T14:20:53+00:00

I have two tables like the below one in my DB. In the first

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:20:53+00:00Added an answer on May 15, 2026 at 2:20 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables like Parent-Child. In Parent table, there are 7211 records. In
I have two tables like below, mysql> select * from Books ; +----+------+------------+----------+----------+ |
I have two MySQL tables named 'nodes' and 'joinTable' like shown below. I need
Say that I have two tables like those: Employers (id, name, .... , deptId).
I have two tables A and B. I would like to delete all the
I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have a mysql problem. I have two tables like this that I need
I have two tables, one for articles/polls, and one for the votes on these
I have two tables named users and contacts . I would like to delete
I have a simple database with two tables: Photo and Tag . There is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.