Oracle SQL - 最后获取一条记录

发布于 2024-11-09 03:12:51 字数 267 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-11-16 03:12:51

添加另一个虚拟列 (order_col) 以用于排序。
查询的结果如下。

SELECT * FROM
(
SELECT city, 1 as order_col from addr_tab
UNION ALL
SELECT 'zzzz', 2 as order_col AS city from dual
)ORDER BY order_col, city

通过这种方式,您可以保证“zzzz”始终位于最后,因为主排序列 (order_col) 的“zzzz”值保证低于所有其余记录。

Add another virtual column (order_col) to use for ordering.
The query would end up something as follows.

SELECT * FROM
(
SELECT city, 1 as order_col from addr_tab
UNION ALL
SELECT 'zzzz', 2 as order_col AS city from dual
)ORDER BY order_col, city

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.

九八野马 2024-11-16 03:12:51

好吧,正如其他人所说(在评论和回答中),可能有更好的方法从一开始就解决问题;但如果您需要临时破解,请继续阅读!...

将最后一条记录命名为您想要的任何名称...
“zzzz”很好...
然后使用自定义的 ORDER BY ,例如:

SELECT * 
  FROM (
       SELECT city 
         FROM addr_tab
       UNION ALL
       SELECT 'zzzz' AS city 
         FROM dual
       )
ORDER BY 
    CASE WHEN city = 'zzzz' THEN 1 ELSE 0 END,
    city;

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:

SELECT * 
  FROM (
       SELECT city 
         FROM addr_tab
       UNION ALL
       SELECT 'zzzz' AS city 
         FROM dual
       )
ORDER BY 
    CASE WHEN city = 'zzzz' THEN 1 ELSE 0 END,
    city;
起风了 2024-11-16 03:12:51

这可以通过以下查询来解决,

 SELECT * FROM 
 ( SELECT city from addr_tab order by city)
   UNION ALL 
  SELECT 'zzzz' from dual;

现在还多了一种样式来执行此操作,

with 
b1 as 
(
SELECT city from addr_tab order by city
)
select city from b1
union all 
SELECT 'zzzz' from dual;

因此您可以使用任何一种......

this can be solve by following query also

 SELECT * FROM 
 ( SELECT city from addr_tab order by city)
   UNION ALL 
  SELECT 'zzzz' from dual;

now got one more style to do this

with 
b1 as 
(
SELECT city from addr_tab order by city
)
select city from b1
union all 
SELECT 'zzzz' from dual;

so any one you can use ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文