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?
There are two steps involved to get this output: a join and a pivot.
An example:
First create your sample tables:
First step is the join. Here I use ANSI join syntax, but you can use good old Oracle join syntax as well.
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:
Hope this helps.
Regards,
Rob.