MySQL计算周环比
表一:查询当天和一周前的日期
select Date, date_sub(Date, interval 7 day) last_date, Impressions, Clicks, Conversions from google_ads_campaign_performance_report
表二:
select Date, Impressions, Clicks, Conversions from google_ads_campaign_performance_report
以一周前的日期为主键进行拼接:
select this.Date, last.Date last_date, this.Impressions, this.Clicks, this.Conversions, last.Impressions last_impressions, last.Clicks last_clicks, last.Conversions last_conversions, ifnull(round((this.Clicks-last.Clicks)/last.Clicks,2),0) week_radio from
(select Date, date_sub(Date, interval 7 day) last_date, Impressions, Clicks, Conversions from google_ads_campaign_performance_report) this left join
(select Date, Impressions, Clicks, Conversions from google_ads_campaign_performance_report) last
on this.last_date = last.Date;