2010-06-17 107 views
1

有我的分貝ITEM_MASTER,PRICE_MASTER,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 

三個表我想所有這些在單個查詢

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

的ITEM_CODE我怎麼能寫一個神諭SQL查詢這個?

回答

6

獲取此輸出涉及兩個步驟:連接和數據透視。

一個例子:

首先創建示例表:

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. 

第一步是加入。這裏我使用ANSI連接語法,但您也可以使用良好的Oracle連接語法。

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. 

comp說明顯示在彼此下面,但您希望它們彼此相鄰。爲了達到這個目的,你轉換結果集。請注意,你必須硬編碼你想要樞軸的行數:

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. 

希望這會有所幫助。

問候, 羅布。

+0

謝謝你....你救了我的一天.. :-) – Andromeda 2010-06-17 11:46:57

+0

+1,很好的示例代碼。 – DCookie 2010-06-17 21:18:55