对偶的sql查询
这:
select *
from dual
connect by level <= i
...将返回结果为
1
2
3
...
i
可以修改查询以获取逐行结果吗? IE
1 2 3 .....i
This:
select *
from dual
connect by level <= i
...will return result as
1
2
3
...
i
Can the query be modified to get result row wise? i.e
1 2 3 .....i
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的功能称为“透视”——它将行数据转换为列数据。相反的称为“unpivot”。有 PIVOT/UNPIVOT 语法,但直到 Oracle 11g 才可用。
在 Oracle 9i+ 上,使用 CASE 语句,而在此之前您需要使用 DECODE 构建逻辑,以便值按列显示。下面是一个解决 Oracle 9i+ 上 LEVEL 限制为 5 的示例:
需要聚合函数 MAX 将结果集“展平”为单行/记录。
如果您到目前为止,您会想“但我不想为每一列指定 - 我希望它基于 i 是动态的......”。有两个问题:
The functionality you're after is called "pivot"--it's converting row data into columnar data. The opposite is called "unpivot". There is PIVOT/UNPIVOT syntax, but it isn't available until Oracle 11g.
On Oracle 9i+, CASE statements are used while prior to that you need to use DECODE to construct the logic so the values come out in columns. Here's an example for addressing if the limit to LEVEL is five on Oracle 9i+:
The aggregate function MAX is necessary to "flatten" the resultset into a single row/record.
If you got this far, you're thinking "but I don't want to have to specify for every column--I want it to be dynamic based on i...". There's two issues:
试试这个:
更新:测试输出:
另一个更新:我想我在发布之前没有充分阅读所有评论,抱歉。 :) 如果您需要选择每个数字作为单独的列,那么是的,请参阅 OMG Ponies 的答案 - 它是枢轴或动态 SQL。 :)
Try this:
update: Testing the output:
another update: Guess I haven't read all comments well enough before posting, sorry. :) If you need to select each number as a separate column, then yes, see, OMG Ponies' answer - it's either pivot or dynamic SQL. :)