我需要将以下查询的结果转换为行输出。
select 'Purchase','Sale','Discount','Out of Stock' from dual
输出:
Purchase
Sale
Discount
Out of Stock
答案 0 :(得分:0)
您可以使用 UNION 将值设为不同的行。例如,
SQL> WITH DATA(item) AS
2 ( SELECT 'Purchase' FROM dual
3 UNION
4 SELECT 'Sale' FROM dual
5 UNION
6 SELECT 'Discount' FROM dual
7 UNION
8 SELECT 'Out of Stock' FROM dual
9 )
10 SELECT item FROM DATA;
ITEM
------------
Discount
Out of Stock
Purchase
Sale
SQL>
请记住,union不允许重复,因此如果您想允许重复行,请使用 UNION ALL 。
答案 1 :(得分:0)
你必须使用UNPIVOT才能获得它。 UNPIVOT与PIVOT相反,它将列值转换为
with tbl(col1,col2,col3,col4) as
(
select 'Purchase','Sale','Discount','Out of Stock' from dual
),tbl2 as(
SELECT *
FROM tbl UNPIVOT (dat for col in (col1,col2,col3, col4)))
select dat from tbl2
如果还有1行,那么最好填充列名,如下所示。
with tbl(col1,col2,col3,col4) as
(
select 'Purchase','Sale','Discount','Out of Stock' from dual union
select 'foo','bar','data','blah' from dual
)
SELECT *
FROM tbl UNPIVOT (dat for col in (col1,col2,col3, col4));
但是如果你只想要这些值,那么只选择'dat'列,如第一个例子所示。