选择最大总和

时间:2017-05-02 21:52:48

标签: sql

我正在尝试查找查询中的最大总数,并且只能显示所有条目的总数。到目前为止我已编码。

SELECT id "ID number", name "Name", SUM(quantity*cost) "Total"
FROM distributioncenter NATURAL JOIN inventory 
GROUP BY id, name

同样,这会为所有人提取总数,但我无法弄清楚如何获得最大总数。

3 个答案:

答案 0 :(得分:0)

我认为您希望MAX()返回所有

的最高值

答案 1 :(得分:0)

编辑:

 select max(total) as maximum_value from
(SELECT id "ID number", name "Name", SUM(quantity*cost) "Total"
 FROM distributioncenter NATURAL JOIN inventory 
 GROUP BY id, name  
)   

如果嵌套查询不起作用并生成不同的表。

    create table want as  
    SELECT id "ID number", name "Name", SUM(quantity*cost) "Total"
         FROM distributioncenter NATURAL JOIN inventory 
         GROUP BY id, name ;  

select max(total) as maximum_value from want;

答案 2 :(得分:0)

您使用的是哪种SQL?我想你正在寻找一个嵌套的SELECT,许多SQL方言都支持它。在这种情况下,答案是:

SELECT MAX(Total) FROM (
    SELECT id "ID number", name "Name", SUM(quantity*cost) "Total"
    FROM distributioncenter NATURAL JOIN inventory 
    GROUP BY id, name
) AS XX