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 856461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:14:22+00:00 2026-05-15T08:14:22+00:00

There are three tables in my db: ITEM_MASTER, PRICE_MASTER and COMP_MASTER. ITEM_MASTER STORE_CODE ITEM_CODE

  • 0

There are three tables in my db: ITEM_MASTER, PRICE_MASTER and COMP_MASTER.

ITEM_MASTER
STORE_CODE  ITEM_CODE  ITEM_DESC
   011         914004   desccc

PRICE_MASTER
STORE_CODE  ITEM_CODE  COMP_CODE
   011         914004     01 
   011         914004     02
   011         914004     03
   011         914004     04

COMP_MASTER
COMP_CODE   COMP_DESC   STORE_CODE
   01        comp1         011
   02        comp2         011
   03        comp3         011
   04        comp4         011 

I want to get all these for an ITEM_CODE in a single query.

  STORE_CODE ITEM_CODE ITEM_DESC COMP_DESC1 COMP_DESC2 COMP_DESC3 COMP_DESC4
     011       914004     desccc   comp1      comp2       comp3      comp4

How can I write an oracle SQL 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-15T08:14:23+00:00Added an answer on May 15, 2026 at 8:14 am

    There are two steps involved to get this output: a join and a pivot.

    An example:

    First create your sample tables:

    SQL> create table item_master (store_code,item_code,item_desc)
      2  as
      3  select '011', 914004, 'desccc' from dual
      4  /
    
    Table created.
    
    SQL> create table price_master (store_code,item_code,comp_code)
      2  as
      3  select '011', 914004, '01' from dual union all
      4  select '011', 914004, '02' from dual union all
      5  select '011', 914004, '03' from dual union all
      6  select '011', 914004, '04' from dual
      7  /
    
    Table created.
    
    SQL> create table comp_master (comp_code,comp_desc,store_code)
      2  as
      3  select '01', 'comp1', '011' from dual union all
      4  select '02', 'comp2', '011' from dual union all
      5  select '03', 'comp3', '011' from dual union all
      6  select '04', 'comp4', '011' from dual
      7  /
    
    Table created.
    

    First step is the join. Here I use ANSI join syntax, but you can use good old Oracle join syntax as well.

    SQL> select i.store_code
      2       , i.item_code
      3       , i.item_desc
      4       , c.comp_desc
      5    from item_master i
      6         inner join price_master p
      7         on (   i.store_code = p.store_code
      8            and i.item_code = p.item_code
      9            )
     10         inner join comp_master c
     11         on (   p.store_code = c.store_code
     12            and p.comp_code = c.comp_code
     13            )
     14  /
    
    STO  ITEM_CODE ITEM_D COMP_
    --- ---------- ------ -----
    011     914004 desccc comp1
    011     914004 desccc comp2
    011     914004 desccc comp3
    011     914004 desccc comp4
    
    4 rows selected.
    

    The comp description appear below each other, but you want them to be next to each other. To achieve that, you pivot the result set. Note that you have to hard code the number of rows you want to pivot:

    SQL> with t as
      2  ( select i.store_code
      3         , i.item_code
      4         , i.item_desc
      5         , c.comp_desc
      6         , row_number() over (partition by i.store_code,i.item_code order by c.comp_code) rn
      7      from item_master i
      8           inner join price_master p
      9           on (   i.store_code = p.store_code
     10              and i.item_code = p.item_code
     11              )
     12           inner join comp_master c
     13           on (   p.store_code = c.store_code
     14              and p.comp_code = c.comp_code
     15              )
     16  )
     17  select store_code
     18       , item_code
     19       , item_desc
     20       , max(decode(rn,1,comp_desc)) comp_desc1
     21       , max(decode(rn,2,comp_desc)) comp_desc2
     22       , max(decode(rn,3,comp_desc)) comp_desc3
     23       , max(decode(rn,4,comp_desc)) comp_desc4
     24    from t
     25   group by store_code
     26       , item_code
     27       , item_desc
     28  /
    
    STO  ITEM_CODE ITEM_D COMP_ COMP_ COMP_ COMP_
    --- ---------- ------ ----- ----- ----- -----
    011     914004 desccc comp1 comp2 comp3 comp4
    
    1 row selected.
    

    Hope this helps.

    Regards,
    Rob.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, there isn't. You will need to implement your own… May 16, 2026 at 6:33 pm
  • Editorial Team
    Editorial Team added an answer Use the --uninstall or -u option to develop, i.e: python… May 16, 2026 at 6:33 pm
  • Editorial Team
    Editorial Team added an answer If you view a html file from Notepad++, it will… May 16, 2026 at 6:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've two master tables Org & Item and then there's an OrgItem table.I have
I have three tables named Item, Event, and Seat, constructed like this: Item Id
I have defined three tables, Stores, InventoryItems and StoreItemRecords. My StoreItemRecords table has foreign
I have 2 Tables . CategoryMaster(CATEGORY_ID (primaryKey),Category_Name) and FILE_MASTER (FileId primaryKey,FILE_TITLE,FILE_DESCRIPTION,CATEGORY_ID (refer to Category
Relevant Tables #One# +----------+ +--------------+ |Quotations| ---> |PurchaseOrders| +----------+ | +--------------+ <One> | <Many>
Is there a way to ensure a particular conditional clause is added to the
SO really there are two parts of the question: 1) how to use CONTAINSTABLE()
I'm implementing a table per subclass design I discussed in a previous question .
I have this Birt report that I inherited from another developer, consisting of a
I am saddled with an ERP database which lacks any foreign keys and therefore

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.