-- 在结果数据中添加序号,简单问题复杂化了;
SELECT
*,
CASE -- 零时编排序号
WHEN id MOD 2 = 0
AND id MOD 4 = 0 THEN
's4'
WHEN ( id + 3 ) MOD 2 = 0
AND ( id + 3 ) MOD 4 != 0 THEN
's3'
WHEN id MOD 2 = 0
AND id MOD 4 != 0 THEN
's2'
WHEN ( id + 3 ) MOD 2 = 0
AND ( id + 3 ) MOD 4 = 0 THEN
's1' ELSE '0'
END 'd' -- case 判断属于s1-s4
FROM
(
select
*,
avg,
@rank:=@rank + 1 as id
from
(
SELECT
*, -- 分组字段写全
avg( t ) avg
FROM
(
SELECT
COLLECTION_TIME,
CONCAT( a2.COLLECTION_TIME, '_', a2.UNIT_NAME ) AS x,
UNIT_NAME,
RESOURCE_NAME,
fMPA_CuGain AS t
FROM
parameter_pid106a_02 a2
INNER JOIN ( SELECT UNIT_NAME AS dmc FROM parameter_pid106a_02 GROUP BY UNIT_NAME ORDER BY COLLECTION_TIME DESC ) a1 ON a2.UNIT_NAME = a1.dmc
) a3
GROUP BY
A3.UNIT_NAME -- 分组字段写全
ORDER BY
COLLECTION_TIME DESC
) a4,(SELECT @rank:= 0) kk
) mm