我在sql里面写了一段sql解决了这个问题,思路是按条件筛选前二十调并求和:SELECT
年月,
'宁波整箱绩效组' AS 绩效组,
'宁波整箱操作' AS 绩效类型,
人,
'宁波' AS 项,
'宁波整箱票数' AS 目,
'每月扣减数字' AS 类,
NULL AS 别,
-1.00 AS 绩效系数,
NULL AS 固定补贴,
SUM(绩效系数) AS 值
FROM (
SELECT
年月,
人,
绩效系数,
CASE
WHEN 类 = '一票到底自拉自报' THEN 1
WHEN 类 = '一票到底代拉代报' THEN 2
WHEN 类 = '仅代拉代报' THEN 3
WHEN 类 = '合作自拉自报' THEN 4
WHEN 类 = '合作代拉代报' THEN 5
ELSE 6
END AS 排序值,
@row_number := IF(@prev_year_month_person = CONCAT(年月, '_', 人), @row_number + 1, 1) AS row_num,
@prev_year_month_person := CONCAT(年月, '_', 人)
FROM
zh_nbty.单证,
(SELECT @row_number := 0, @prev_year_month_person := NULL) AS vars
WHERE
绩效类型 = '宁波整箱操作'
ORDER BY
年月, 人, 排序值
) AS sorted_data
WHERE
row_num <= 20
GROUP BY
年月, 人 |