如何只显示select的第一个结果?

时间:2017-05-14 14:29:23

标签: sql oracle greatest-n-per-group

我试图仅显示此选择的第一个结果,但无法弄清楚如何操作或应该使用哪种功能。

我尝试了 rownum< = 1 ,但它改变了显示结果。

select name || ' ' || surname as meno, role
from system_user
order by surname ASC
;

1 个答案:

答案 0 :(得分:4)

在Oracle中,您需要使用子查询:

select su.*
from (select name || ' ' || surname as meno, role
      from system_user
      order by surname ASC
     ) su
where rownum = 1;

最新版本的Oracle(12C)通过支持ANSI标准FETCH FIRST 1 ROW ONLY语法使这更简单:

select name || ' ' || surname as meno, role
from system_user
order by surname ASC
fetch first 1 row only;