記錄一條 環(huán)比統(tǒng)計sql
需求:統(tǒng)計22年1月到6月,每月的數(shù)量昵观,同比上月增長百分比
SET @rownum1 = 0 ;
SET @rownum2 = 0 ;
select m.month AS '月份', m.count as '本月數(shù)量', g.count AS '上月數(shù)量', (m.count - g.count) / g.count as '環(huán)比'
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);