行与列相反
我有一个更大的 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 次来获得此结果。 我正在寻求建议,联合是否是我在这里能做的最好的事情,以及是否可以通过其他方式实现此输出。
我还找到了许多将列反转为行的示例,但在这里我正在寻找从行到列的反转。
I have a larger SQL that produces a similar output as this simplified example:
SELECT
5 AS L0,
2 AS L1,
3 AS L2,
4 AS L3
FROM DUAL
current output is this row:
| L0 | L1 | L2 | L3 |
| 5 | 2 | 3 | 4 |
desired output are this columns:
| kind | value |
| 0 | 5 |
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
I know I could get this by union the select 4 times.
I'm looking for advice if union is the best I can do here and if this output can be achieved by other means.
I also found many examples to inverse a column to a row but here I'm looking for inversion from a row to column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试UnPIVOT:
反向:
假设您实际上有一个可以查询的表。
Please try UnPIVOT:
Inversely:
Assuming that you actually have a table to query from.
<代码>
This does a Carteasan Product of your table and the "inline view." The inline view will output four rows, 1 through 4.
This does a Carteasan Product of your table and the "inline view." The inline view will output four rows, 1 through 4.