2017-04-25 134 views

回答

0

你可以做這樣的事情:

select max(case when id = 1 then value end), 
     max(case when id = 2 then value end), 
     max(case when id = 3 then value end) 
into out_x, out_y, out_z 
from t 
where id in (1, 2, 3); 

不過,我認爲形式的三個查詢:

select value into out_x 
from t 
where id = 1; 

是一個更簡潔的方法。

0

您可以使用PIVOT

SELECT x, y, z 
INTO out_x, out_y, out_z 
FROM your_table 
PIVOT (MAX(value) FOR id IN (1 AS x, 2 AS y, 3 AS z)) 

或者,如果你不知道你需要哪些ID S(而只是希望第一個3)然後:

SELECT x, y, z 
INTO out_x, out_y, out_z 
FROM (
     SELECT value, ROWNUM AS rn 
     FROM (SELECT value FROM your_table ORDER BY id) 
     WHERE ROWNUM <= 3 
     ) 
PIVOT (MAX(value) FOR rn IN (1 AS x, 2 AS y, 3 AS z)) 
相關問題