我有一个较大的SQL,它产生的输出与此简化示例类似:
SELECT 5 AS L0, 2 AS L1, 3 AS L2, 4 AS L3 FROM DUAL
当前输出是此行:
| L0 | L1 | L2 | L3 | | 5 | 2 | 3 | 4 |
所需的输出是此列:
| kind | value | | 0 | 5 | | 1 | 2 | | 2 | 3 | | 3 | 4 |
我知道我可以通过选择4次来得到这个。我正在寻找建议,如果工会是我在这里可以做的最好的事情,并且该输出是否可以通过其他方式实现。
我还发现了许多将一列反转为一行的示例,但是在这里,我正在寻找从一列反转为一列的例子。
请尝试UnPIVOT:
SELECT substr(kind,2,1) AS kind,"value" FROM ( SELECT 5 AS l0, 2 AS l1, 3 AS l2, 4 AS l3 FROM dual )temp unpivot include NULLS ("value" FOR kind IN (l0, l1 , l2, l3));
反之:
SELECT * FROM ( SELECT Kind, max(Value) Value FROM table GROUP BY Kind ) PIVOT ( max(Value) FOR Kind IN (L0, L1, L2, L3) );
假设您实际上有一个要查询的表。