如何将查询返回的2行放入Oracle sql中单行的2列中

时间:2016-07-15 22:24:00

标签: oracle oracle11g

我运行一个返回2行的查询

SELECT a FROM TABLE-A WHERE condition=something;
  

row1 Value1

     

row2 Value2

现在,我想把它放在一个单行的2列的新表中

SELECT column1, column2 FROM TABLE-B WHERE condition=something;
  

row1 column1 column2

   value1   value2

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

目前还不清楚你想如何选择哪一行进入哪一列 - 这里有几个选项:

SELECT MIN( a ) AS minimum_value,
       MAX( a ) AS maximum_value
FROM   table_a
WHERE  1=1;

SELECT MAX( a ) KEEP ( DENSE_RANK FIRST ORDER BY ROWNUM ) AS first_value,
       MAX( a ) KEEP ( DENSE_RANK LAST  ORDER BY ROWNUM ) AS last_value
FROM   table_a
WHERE  1=1;

SELECT *
FROM   ( SELECT a, ROWNUM AS rn FROM table_a WHERE 1=1 )
PIVOT  ( MAX(a) FOR rn IN ( 1 AS first_value, 2 AS second_value ) );