我有一个如下表:
part_num | type | color | material
_________|_______|_______|_____________
1234 | filter| white | steel
此表具有部件号,部件类型,颜色和材料。
如何旋转/转置表格以获取以下输出:
part |AttrName| AttrValue
_____|________|__________
1234 |type | filter
1234 |color | white
1234 |material| steel
Oracle数据库12c-12.2.2 我以前没有使用过枢轴或未枢轴,因此最好进行解释以了解它们之间的区别。
谢谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以在oracle 11g中使用UNPIVOT
,但是如果您的版本较低,则可以使用UNION ALL
SELECT *
FROM t UNPIVOT (AttrValue FOR AttrName IN(
type,color,material))
输出:
ART_NUM ATTRNAME ATTRVALUE
1234 TYPES filter
1234 COLOR white
1234 MATERIAL steel
答案 2 :(得分:0)
Oracle 12C支持横向联接,因此我将使用:
select t.part_num, x.*
from t cross apply
(select 'type' as AttrName, "type" as AttrValue from dual union all
select 'color', color from dual union all
select 'material', material from dual
) x;
Here是db <>小提琴。
请注意,Oracle还支持以下语法:
select t.part_num, x.*
from t cross join lateral
(select 'type' as AttrName, "type" as AttrValue from dual union all
select 'color', color from dual union all
select 'material', material from dual
) x;