请问mysql 如何分组然后取前10名

1需求: 根据分拨进行分组,然后再取每个分拨的金额排在前10的公司。图片说明
2在回答里面,有几位兄台查询句子里面出现抽取条件,请问这个抽取条件是什么

 SELECT
    a.*
FROM
    TABLE AS a
WHERE
    (
        SELECT
            COUNT(*)
        FROM
            TABLE
        WHERE
            分拨 = a.分拨
        AND 总金额 > a.总金额
    ) < 10
ORDER BY
    总金额

SELECT
分拨,
公司代码,
公司简称,
公司类型,
总金额
FROM 表名
WHERE
抽取条件
GROUP BY 分拨
HAVING COUNT(分拨)<=10
ORDER BY 总金额

假设公司表定义:
company(id:主键ID, name:名称, type:分拨, amt:金额)

 select id, type, name, amt 
from company c
inner join (
select type, (
select case when (select count(*) from company a where a.type = b.type) > 9 
then (select id from company a where a.type = b.type order by amt desc limit 1 offset 9)
else (select id from company a where a.type = b.type order by amt desc limit 1) end
) min_id
from company b
group by type
) d on d.type = c.type
where c.id >= d.min_id
order by type
1. 建立一个临时表company存储需要查询出的数据
2. 循环查出所有的分拨(DECLARE cur CURSOR for select distinct 分拨 from 表名)
3. 遍历所有的分拨,查询每个分拨金额排行的前十名,并插入临时表
OPEN cur;
read_loop: LOOP
FETCH cur INTO 分拨参数(自定义,存储遍历后当前的分拨类别的值);
        insert into company
        select 分拨,公司代码,公司简称,公司类型,金额 from 表名 where 分拨 = 分拨参数(自定义) order by 金额 desc limit 10;
end loop;
close cur;
4. 查询临时表company即可
分拨,公司代码,公司简称,
total 

FROM
(
SELECT
@r :=
CASE

WHEN @分拨 =分拨 THEN
@r + 1 ELSE 1 
END r,
@分拨 :=分拨 分拨,公司简称,公司代码,
total 

FROM
(
SELECT
jan1.分拨,
jan1.公司代码,
jan1.公司简称,
round( sum( jan1.金额 ), 2 ) total
FROM
jan1
WHERE
jan1.公司代码 IS NOT NULL
GROUP BY
jan1.分拨,
jan1.公司代码,
jan1.公司简称
) AS aa,
( SELECT @r := 1, @分拨 := 0 ) bb
ORDER BY
分拨,total DESC
) ab
WHERE
r <= 10;

前面的有点问题,下面这个才是正确的,思路是按金额降序模拟生成行号,取行号前10的数据

假设公司表定义:
company(id:主键ID, name:名称, type:分拨, amt:金额)

select line, id, name, type, amt from (
select (
case when 
(select count(*) from company a where a.type = c.type and a.amt = c.amt) > 1
then 
(select count(*) from company a where a.type = c.type and a.amt > c.amt) 
+ 
(select count(*) from company a where a.type = c.type and a.amt = c.amt and a.id <= c.id)
else 
(select count(*) from company a where a.type = c.type and a.amt >= c.amt) end) line, name, type, amt
from company c
) d 
where line <= 10
order by type, amt desc

select top 10 "总金额" from TABLENAME group by "分拨"

SELECT
分拨,
公司代码,
公司简称,
公司类型,
总金额
FROM 表名
WHERE
抽取条件
GROUP BY 分拨
HAVING COUNT(分拨)<=10
ORDER BY 总金额

赞成楼上这个仁兄的
SELECT
分拨,
公司代码,
公司简称,
公司类型,
总金额
FROM 表名
WHERE
抽取条件
GROUP BY 分拨
HAVING COUNT(分拨)<=10
ORDER BY 总金额

select * from 表名 group by 分拨 order by 金额 limit 10

select

字段名
from
表名
where
条件
group
分拨
order by
金额 desc
limit 10