记录一条 环比统计sql
需求:统计22年1月到6月,每月的数量,同比上月增长百分比
SET @rownum1 = 0 ;
SET @rownum2 = 0 ;
select m.month AS '月份', m.count as '本月数量', g.count AS '上月数量', (m.count - g.count) / g.count as '环比'
from (
select @rownum1:=@rownum1+1 as 'id',date_format(f_create_time, '%y-%m') as 'month', count(distinct f_connector_id) as 'count'
from fp_pile_station.t_connector_info
where f_status > 0
and date_format(f_create_time, '%y') = '22'
and f_yn = 1
group by date_format(f_create_time, '%y-%m')) m
inner join
(
select @rownum2:=@rownum2+1 as 'id',date_format(f_create_time, '%y-%m') as 'month', count(distinct f_connector_id) as 'count'
from fp_pile_station.t_connector_info
where f_status > 0
and date_format(f_create_time, '%y-%m') >= '21-12'
and date_format(f_create_time, '%y-%m') <= '22-05'
and f_yn = 1
group by date_format(f_create_time, '%y-%m')) g using (id);