SQL查询问题,查找结果的最大值

时间:2011-04-27 02:35:29

标签: sql oracle oracle10g aggregate-functions

我正在尝试创建一个查找具有最高值的单行的查询。我现在正在Oracle 10G环境中工作。

我首先创建了此查询,其中列出了数量

SELECT aname, sum(rhowmuch * rhowoften) AS Quantity
FROM ration
GROUP BY ration.aname;

ANAME        QUANTITY
---------- ----------
BLACKIE            16
CONRAD           29.4
MEO                28
MOLLY            37.7
CHUBBLES           75
BOSS             63.7
JEFFREY          29.4
FANNY              56
SUZY               21
ZEO              17.5
MORRIS             21

ANAME        QUANTITY
---------- ----------
SALLY              21
LEO                40
SAM                28

14 rows selected.

但我无法找到一种方法来创建一个从中获取最大数量的单个查询。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

使用:

SELECT x.aname, x.quantity
  FROM (SELECT r.name, SUM(r.rhowmuch + r.rhowoften) AS quantity
          FROM RATION r
      GROUP BY r.aname
      ORDER BY quantity DESC) x
 WHERE ROWNUM = 1

有关使用ROWNUM的详细信息,请参阅:http://blog.lishman.com/2008/03/rownum.html

答案 1 :(得分:1)

SELECT aname, sum(rhowmuch * rhowoften) AS Quantity
FROM ration
GROUP BY ration.aname
order by Quantity desc
limit 1