2010-07-07 161 views
2

我一直試圖擺脫使用DECODE來轉換Oracle 11g中的行,其中有一個方便的PIVOT功能。但我可能已經發現了一個限制:Oracle PIVOT,兩次?

我想爲基表中的每個值返回2列。就像:

SELECT somethingId, splitId1, splitName1, splitId2, splitName2 
FROM (SELECT somethingId, splitId 
     FROM SOMETHING JOIN SPLIT ON ...) 
     PIVOT (MAX(splitId) FOR displayOrder IN (1 AS splitId1, 2 AS splitId2), 
       MAX(splitName) FOR displayOrder IN (1 AS splitName1, 2 as splitName2) 
      ) 

我可以用DECODE做到這一點,但我不能摔跤的語法,讓我用PIVOT做。這甚至有可能嗎?似乎這個函數不會太難處理。

編輯:是StackOverflow可能不是正確的SQL溢出問題?

編輯:任何人在那裏?

+0

來吧,有沒有是或否的答案?我在Oracle SQL的錯誤論壇上? – orbfish 2010-07-08 14:41:24

+0

直到創建了http://area51.stackexchange.com/proposals/4260/databases或http://area51.stackexchange.com/proposals/6223/oracle-databases,您才能找到合適的位置。 – 2010-11-09 15:05:41

+0

不幸的是,PIVOT在Oracle中是非常新的,似乎這裏的大多數人都不知道回答你的問題。你如何嘗試發佈到forums.oracle.com?更多的Oracle專家去那裏比來這裏。 – 2010-11-09 15:14:18

回答

2

oracle-developer.net它會出現,它可以這樣做:

SELECT somethingId, splitId1, splitName1, splitId2, splitName2 
FROM (SELECT somethingId, splitId 
     FROM SOMETHING JOIN SPLIT ON ...) 
     PIVOT (MAX(splitId) , 
       MAX(splitName) 
       FOR displayOrder IN (1 AS splitName1, 2 as splitName2) 
      ) 
+0

就是這樣,忘記了先前接受。謝謝! – orbfish 2014-01-21 18:45:21

1

我不知道你提供了什麼樣的數據,或者你想要什麼。也許如果您發佈的查詢的解碼版本返回您正在查找的數據和/或源數據的定義,我們可以更好地回答您的問題。這樣的事情將是有益的:

create table something (somethingId Number(3), displayOrder Number(3) 
    , splitID Number(3));    
insert into something values (1, 1, 10); 
insert into something values (2, 1, 11); 
insert into something values (3, 1, 12); 
insert into something values (4, 1, 13); 
insert into something values (5, 2, 14); 
insert into something values (6, 2, 15); 
insert into something values (7, 2, 16); 

create table split (SplitID Number(3), SplitName Varchar2(30)); 
insert into split values (10, 'Bob'); 
insert into split values (11, 'Carrie'); 
insert into split values (12, 'Alice'); 
insert into split values (13, 'Timothy'); 
insert into split values (14, 'Sue'); 
insert into split values (15, 'Peter'); 
insert into split values (16, 'Adam'); 

SELECT * 
FROM (
    SELECT somethingID, displayOrder, so.SplitID, sp.splitname 
     FROM SOMETHING so JOIN SPLIT sp ON so.splitID = sp.SplitID 
) 
PIVOT (MAX(splitId) id, MAX(splitName) name 
    FOR (displayOrder, displayOrder) IN ((1, 1) AS split, (2, 2) as splitname) 
);