2011-12-16 65 views
2

我可以使用Oracle中的PIVOT關鍵字通過使用單個查詢而不是兩個查詢來創建多行的多個列,如下所示。 例如..Oracle中的樞軸關鍵字

1)

SELECT 
    * 
FROM 
(
SELECT elig.person_id,elig.cmpo_key, recm.pyin_amt FROM sp_cmpo_elig elig, sp_base_pay_recm recm 
    WHERE elig.cmpo_elig_key=recm.cmpo_elig_key AND elig.person_id='807114') temp 
pivot (Max(temp.pyin_amt) FOR (cmpo_key) IN (1 AS pyin_amt_1, 2 AS pyin_amt_2, 3 AS pyin_amt_3, 4 AS pyin_amt_4)) 

如果使用此查詢,我正在像下面的輸出..

PERSON_ID PYIN_AMT_1 PYIN_AMT_2 PYIN_AMT_3 PYIN_AMT_4 
807114  1524.23  235.25 235.25  235.25 

2)

SELECT 
    * 
FROM 
(
SELECT elig.person_id,elig.cmpo_key, recm.pyin_pct FROM sp_cmpo_elig elig, sp_base_pay_recm recm 
    WHERE elig.cmpo_elig_key=recm.cmpo_elig_key AND elig.person_id='807114') temp 
pivot (Max(temp.pyin_pct) FOR (cmpo_key) IN (1 AS pyin_pct_1, 2 AS pyin_pct_2, 3 AS pyin_pct_3, 4 AS pyin_pct_4)) 

輸出:

PERSON_ID PYIN_PCT_1 PYIN_PCT_2 PYIN_PCT_3 PYIN_PCT_4 
807114  2    5   5   3 

我可以如上創建單個查詢而不是兩個查詢。請爲此提供解決方案。

注:本人與工會也試過,如果我使用union它會給兩個記錄像下面...

PERSON_ID PYIN_AMT_1 PYIN_AMT_2 PYIN_AMT_3 PYIN_AMT_4 
807114  1524.23  235.25 235.25  235.25 
807114  2    5  5    3 

我期望的輸出上面不喜歡的,我期待像下面....

PERSON_ID PYIN_AMT_1 PYIN_AMT_2 PYIN_AMT_3 PYIN_AMT_4 PYIN_PCT_1 PYIN_PCT_2 PYIN_PCT_3 PYIN_PCT_4 
807114  1524.23  235.25 235.25  235.25  2    5  5    3 

回答

0
SELECT 
    * 
FROM 
(
SELECT elig.person_id,elig.cmpo_key, recm.pyin_amt FROM sp_cmpo_elig elig, sp_base_pay_recm recm 
    WHERE elig.cmpo_elig_key=recm.cmpo_elig_key AND elig.person_id='807114') temp 
pivot (Max(temp.pyin_amt) FOR (cmpo_key) IN (1 AS pyin_amt_1, 2 AS pyin_amt_2, 3 AS pyin_amt_3, 4 AS pyin_amt_4)) 
UNION 
SELECT 
    * 
FROM 
(
SELECT elig.person_id,elig.cmpo_key, recm.pyin_pct FROM sp_cmpo_elig elig, sp_base_pay_recm recm 
    WHERE elig.cmpo_elig_key=recm.cmpo_elig_key AND elig.person_id='807114') temp 
pivot (Max(temp.pyin_pct) FOR (cmpo_key) IN (1 AS pyin_pct_1, 2 AS pyin_pct_2, 3 AS pyin_pct_3, 4 AS pyin_pct_4)) 
+0

嗨,我嘗試像什麼已給,但它會返回兩個記錄。但我需要該員工的單一記錄,像下面的8列...... – 2011-12-16 09:13:01