CREATE TABLE `DEMO`(
`b_id` INT NOT NULL COMMENT '业务员 ID',
`c_id` INT NOT NULL COMMENT '客户 ID',
`sale` INT NOT NULL COMMENT '价格'
);
INSERT INTO `DEMO` (`b_id`, `c_id`, `sale`)
VALUES (1, 2, 1),
(1, 2, 1),
(1, 3, 1),
(1, 3, 1),
(1, 4, 1),
(1, 4, 1),
(1, 5, 1),
(1, 5, 1),
(1, 6, 1),
(2, 1, 3),
(2, 1, 1),
(2, 2, 3),
(2, 2, 1),
(2, 4, 3),
(2, 5, 1),
(2, 6, 3),
(2, 6, 1),
(3, 1, 3),
(3, 1, 1),
(3, 2, 3),
(3, 2, 1),
(3, 4, 3),
(3, 5, 1),
(3, 6, 3),
(3, 6, 1);
SELECT * FROM `DEMO`;
+------+------+------+
| b_id | c_id | sale |
+------+------+------+
| 1 | 2 | 1 |
| 1 | 2 | 1 |
| 1 | 3 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 1 |
| 1 | 4 | 1 |
| 1 | 5 | 1 |
| 1 | 5 | 1 |
| 1 | 6 | 1 |
| 2 | 1 | 3 |
| 2 | 1 | 1 |
| 2 | 2 | 3 |
| 2 | 2 | 1 |
| 2 | 4 | 3 |
| 2 | 5 | 1 |
| 2 | 6 | 3 |
| 2 | 6 | 1 |
| 3 | 1 | 3 |
| 3 | 1 | 1 |
| 3 | 2 | 3 |
| 3 | 2 | 1 |
| 3 | 4 | 3 |
| 3 | 5 | 1 |
| 3 | 6 | 3 |
| 3 | 6 | 1 |
+------+------+------+
SELECT `b_id`, `c_id`, `sales`
FROM (SELECT `b_id`, `c_id`, `sales`, RANK() OVER(PARTITION BY `b_id` ORDER BY `sales` DESC) as level
FROM (SELECT `b_id`, `c_id`, SUM(`sale`) AS `sales` FROM DEMO GROUP BY `b_id`, `c_id`) SUM_DEMO
order by `b_id`) LEVEL_DEMO
WHERE level <= 10;
+------+------+-------+
| b_id | c_id | sales |
+------+------+-------+
| 1 | 2 | 2 |
| 1 | 3 | 2 |
| 1 | 4 | 2 |
| 1 | 5 | 2 |
| 1 | 6 | 1 |
| 2 | 1 | 4 |
| 2 | 2 | 4 |
| 2 | 6 | 4 |
| 2 | 4 | 3 |
| 2 | 5 | 1 |
| 3 | 1 | 4 |
| 3 | 2 | 4 |
| 3 | 6 | 4 |
| 3 | 4 | 3 |
| 3 | 5 | 1 |
+------+------+-------+