Mysql按某个字段实现分组统计Top5 Sql语法查询语句
SET @id:= 0,@type1:= '';
SELECT dsx.row_number, dsx.type, dsx.type_name, dsx.SKU, dsx.number
FROM
(
SELECT b.type, b.type_name, b.SKU, b.number,
@id:= IF(@type1 = type,@id+1,1) as row_number, @type1:= type
FROM
(
SELECT a.type, a.type_name, a.SKU, a.number
FROM
(
SELECT "01" AS type,"01类别" AS type_name, "00000001" AS SKU, 213 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000002" AS SKU, 234 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000003" AS SKU, 345 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000004" AS SKU, 454 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000005" AS SKU, 223 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000006" AS SKU, 123 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000007" AS SKU, 321 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000008" AS SKU, 741 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000009" AS SKU, 463 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000010" AS SKU, 127 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000011" AS SKU, 147 AS number UNION ALL
SELECT "01" AS type,"01类别" AS type_name, "00000012" AS SKU, 134 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000013" AS SKU, 129 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000014" AS SKU, 342 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000015" AS SKU, 232 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000016" AS SKU, 444 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000017" AS SKU, 555 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000018" AS SKU, 666 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000019" AS SKU, 332 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000020" AS SKU, 234 AS number UNION ALL
SELECT "02" AS type,"02类别" AS type_name, "00000021" AS SKU, 532 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000022" AS SKU, 121 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000023" AS SKU, 231 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000024" AS SKU, 875 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000025" AS SKU, 587 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000026" AS SKU, 569 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000027" AS SKU, 487 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000028" AS SKU, 478 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000029" AS SKU, 587 AS number UNION ALL
SELECT "03" AS type,"03类别" AS type_name, "00000030" AS SKU, 789 AS number
) AS a
ORDER BY a.type, a.number DESC
) AS b
) AS dsx
WHERE dsx.row_number <=5