sql - 行到列数据
问题描述
我有表名为AA
. 我有 3 列,即x,y,z
。它只有一排。
select * from aa;
x y z
10 20 30
我想要像这样的输出
10
20
30
我使用了以下查询
select x from AA union all select y from AA union all select z from AA ;
它正在提供所需的输出。但有人告诉我这是不可行的查询。你们中的任何人都可以为我提供最佳解决方案。
解决方案
您的查询很好:
select x from AA union all
select y from AA union all
select z from AA ;
更高效的版本更长一点:
select (case when n = 1 then x
when n = 2 then y
else z
end) as x
from (select 1 as n from dual union all select 2 union all select 3
) cross join
AA;
最新版本的 Oracle 支持横向连接和apply
. 如果您正在使用一个,我建议:
select d(x)
from aa cross apply
(select aa.x from dual union all
select aa.y from dual union all
select aa.z from dual
) d
推荐阅读
- flutter - 无法访问 JSON 数据
- pytorch - 需要帮助转换 pytorch 张量
- javascript - 无法通过 selenium javascript 找到 react-select 下拉元素
- mmenu - 带有文本输入字段的 mmenu 在单击时失去对 Android 设备的关注,导致菜单关闭
- node.js - 将 github 页面上托管的网站映射到 /etc/hosts 后,无法将其重定向到 localhost
- javascript - 网站上的问题..注册仍然出现在登录表单上
- windows-10 - 你如何为 MS Teams 制定 Windows Defender 防火墙规则?
- javascript - 过滤函数在reactjs中返回空数组
- sql-server - 死锁 - 从死锁报告中查找完整的 SQL 语句
- laravel - Laravel 获取模型可用座位