SELECT org, pre_org, stat_year, stat_week, str_week, gzbx_num, sq_type, stat_ym, @rownum:=@rownum+1 AS rownum, '去年' AS year_type,rank FROM (SELECT org, pre_org, stat_year, stat_week, str_week, gzbx_num, sq_type, stat_ym, CASE WHEN @stat_ym = stat_ym THEN @rank WHEN @stat_ym := stat_ym THEN @rank := @rank+1 ELSE @rank := @rank + 1 END AS rank , @stat_ym = stat_ym FROM (SELECT regexp_replace(org, '供电所',null) AS org ,pre_org ,stat_year ,stat_week , concat(stat_week,'周') str_week ,gzbx_num ,sq_type ,concat(stat_year,lpad(stat_week,2,0)) stat_ym FROM RC_FPO_OUTAGE_REQUEST_2 WHERE sq_type = '抱怨类' AND stat_year !=2019 ORDER BY stat_year,stat_week,stat_ym) b, (SELECT @stat_ym :=null, @rank := 0)t WHERE 1 =1 ORDER BY stat_ym ) a, (SELECT @rownum:=0)t WHERE rank = 1 |