如何从表中获取一组值的SUM,然后获得这些值的MAX并回显5个最高值

时间:2015-02-01 15:45:18

标签: mysql

预警,我是一个新手,所以要温和。我需要从一组值中得到 qty 的SUM,然后是前5个MAX sum qty 从那个名单。但是我需要从最终列表中省略一个NULL值。

2 个答案:

答案 0 :(得分:1)

我会在没有子查询的情况下执行此操作:

select game_name, sum(qty) AS total 
from sales
where game_name is not null
group by game_name
order by total desc
limit 5;

答案 1 :(得分:0)

试试这个:

SELECT game_name,
       max(total) AS total  
FROM ( select game_name,
              sum(qty) AS total
       FROM sales
       GROUP BY game_name
     ) AS RANK
where game_name is not null
group by game_name
order by total desc limit 5 ;

它将删除任何null游戏,按总降序排序,它会为您提供前5行。