Skip to content
Advertisement

Oracle join query

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement