从表中获取最高价值

时间:2015-09-28 18:37:40

标签: sql oracle max sql-limit

我试图只显示第一行,在MYSQL中的例子是:

ORDER BY foo DESC LIMIT 1; 

我相信oracle不使用LIMIT子句。我试过了ROWNUM = 1,但似乎没有用。这是显示第一行的正确方法吗?

select customer_name, 
       MAX(balance) as "Highest Depositor Value"
  from depositor
 inner join account
    on depositor.account_number = account.account_number
 group by customer_name, balance
 order by balance
 where rownum = 1;


ERROR at line 4:
ORA-00933: SQL command not properly ended

我得到了答案!感谢

select customer_name,max(balance) as "Highest Depositor Value"
 from depositor
 inner join account
    on depositor.account_number = account.account_number
 group by customer_name, balance order by balance desc
 fetch first 1 rows only;


CUSTOMER_NAME   Highest Depositor Value
 --------------- -----------------------
 Lindsay                          100000

2 个答案:

答案 0 :(得分:1)

删除此部分:

order by balance where rownum = 1

你应该有什么工作..

MAX会给你最大的...你不需要告诉它有多少行,MAX只给出1个最大值。

[编辑] 如果您只想要1个最大值,则需要通过..删除组/订单,并从select中删除“客户名称”:

  select MAX(balance) as "Highest Depositor Value"
  from depositor
  inner join account on depositor.account_number = account.account_number;

[/编辑]

答案 1 :(得分:1)

我在这里找到了答案。

How do I limit the number of rows returned by an Oracle query after ordering?

  select customer_name,max(balance) as "Highest Depositor Value"
2  from depositor
3  inner join account on depositor.account_number = account.account_number
4  group by customer_name, balance order by balance desc
5  fetch first 1 rows only;
CUSTOMER_NAME   Highest Depositor Value
 --------------- -----------------------
 Lindsay                          100000