Oracle SQL - 最后获取一条记录
SELECT * FROM
(
SELECT city from addr_tab
UNION ALL
SELECT 'zzzz' AS city from dual
)ORDER BY city
我正在使用上述方法来检索“zzzz”作为表的最后一条记录。但我认为这不会一直有效,因为从理论上讲,这座城市可能比按字母顺序排列的“zzzz”更大。是否有其他可靠的方法来将此编码记录作为最后一条记录?我需要这个来使用 Oracle 报告进行报告。任何帮助将是非常受欢迎的。
SELECT * FROM
(
SELECT city from addr_tab
UNION ALL
SELECT 'zzzz' AS city from dual
)ORDER BY city
I am using the above approach to retrieve the 'zzzz' as last record of the table. But I don't think this would work all the time, as the city theoretically might be something bigger than "zzzz" alphabetically. Is there any other robust approach to get this harcoded record as the last record?. I need this for reporting using oracle reports. Any help would be most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加另一个虚拟列 (order_col) 以用于排序。
查询的结果如下。
通过这种方式,您可以保证“zzzz”始终位于最后,因为主排序列 (order_col) 的“zzzz”值保证低于所有其余记录。
Add another virtual column (order_col) to use for ordering.
The query would end up something as follows.
THis way you can guarantee that 'zzzz' will always be last since the primary ordering column (order_col) is guaranteed to have a lower value for 'zzzz' than for all the rest of the records.
好吧,正如其他人所说(在评论和回答中),可能有更好的方法从一开始就解决问题;但如果您需要临时破解,请继续阅读!...
将最后一条记录命名为您想要的任何名称...
“zzzz”很好...
然后使用自定义的
ORDER BY
,例如:Well, as others have said (in comment and answer) there is probably a better way to approach the problem from the beginning; but if you need a temporary hack read on!...
Call the last record whatever you want...
'zzzz' is fine...
and then use a custom
ORDER BY
like:这可以通过以下查询来解决,
现在还多了一种样式来执行此操作,
因此您可以使用任何一种......
this can be solve by following query also
now got one more style to do this
so any one you can use ....