Mysql同比环比统计
1.同比查询(今天和昨天相比)
select DATE_FORMAT(t5.date,'%Y/%m/%d') day,SUM(t5.newhan) num, CASE WHEN SUM(t5.oldhan) IS NULL OR SUM(t5.oldhan) = 0 THEN 0 WHEN SUM(t5.newhan) IS NULL OR SUM(t5.newhan) = 0 THEN 0 ELSE CONCAT(FORMAT(((t5.newhan-t5.oldhan)/t5.oldhan)*100,2),'%') END yoy from ( select t3.day as date,t3.day_pv as newhan,t2.day_pv1 as oldhan from (select date_format(start_time, '%Y-%m-%d') as day,sum(pv) as day_pv from page_data group by day) t3 left join ( select date_add(t1.day,INTERVAL 1 DAY) as last_day,t1.day_pv as day_pv1 from (select date_format(start_time, '%Y-%m-%d') as day,sum(pv) as day_pv from page_data group by day) t1 ) t2 on t3.day=t2.last_day ) t5 WHERE DATE_FORMAT(t5.date,'%Y-%m-%d') between '2020-06-01' and '2020-07-24' GROUP BY t5.date
注:
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。 %Y:年,4 位; %m:月,数值(00-12); %d: 月的天,数值(00-31). FORMAT()函数:可以将数据格式化为整数或者带几位小数的浮点数(四舍五入); CONCAT(str1,str2,…): 返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。
INTERVAL 代表的是时间间隔
同比结果
2.同比环比联合查询(环比,今天和上周相比)
select DATE_FORMAT(t5.date,'%Y/%m/%d') day,SUM(t5.newhan) num, CASE WHEN SUM(t5.oldhan) IS NULL OR SUM(t5.oldhan) = 0 THEN 0 WHEN SUM(t5.newhan) IS NULL OR SUM(t5.newhan) = 0 THEN 0 ELSE CONCAT(FORMAT(((t5.newhan-t5.oldhan)/t5.oldhan)*100,2),'%') END yoy, CASE WHEN SUM(t5.oldhan7) IS NULL OR SUM(t5.oldhan7) = 0 THEN 0 WHEN SUM(t5.newhan) IS NULL OR SUM(t5.newhan) = 0 THEN 0 ELSE CONCAT(FORMAT(((t5.newhan-t5.oldhan7)/t5.oldhan7)*100,2),'%') END mom from ( select t3.day as date,t3.day_pv as newhan,t2.day_pv1 as oldhan,t7.day_pv7 oldhan7 from (select date_format(start_time, '%Y-%m-%d') as day,sum(pv) as day_pv from page_data group by day) t3 left join ( select date_add(t1.day,INTERVAL 1 DAY) as last_day,t1.day_pv as day_pv1 from (select date_format(start_time, '%Y-%m-%d') as day,sum(pv) as day_pv from page_data group by day) t1 ) t2 on t3.day=t2.last_day LEFT JOIN ( select date_add(t1.day7,INTERVAL 7 DAY) as last_day7,t1.day_pv7 as day_pv7 from (select date_format(start_time, '%Y-%m-%d') as day7,sum(pv) as day_pv7 from page_data group by day7) t1 ) t7 ON t3.day=t7.last_day7 ) t5 WHERE DATE_FORMAT(t5.date,'%Y-%m-%d') between '2020-06-01' and '2020-07-24' GROUP BY t5.date
同比环比联合查询返回结果
3.时间段内同比环比统计查询
SELECT '浏览量' text,DATE_FORMAT('2020-06-01','%Y/%m/%d') startTime,DATE_FORMAT('2020-07-24','%Y/%m/%d') endTime,SUM(tpv.num) number,CONCAT(FORMAT(SUM(tpv.yoy)*100,2),'%') yoys,CONCAT(FORMAT(SUM(tpv.mom)*100,2),'%') moms FROM (select t5.date pvDate,t5.eTime pveTime,SUM(t5.newhan) num, ((t5.newhan-t5.oldhan)/t5.oldhan) yoy, ((t5.newhan-t5.oldhan7)/t5.oldhan7) mom from ( select t3.day as date,t3.day_pv as newhan,t2.day_pv1 as oldhan,t7.day_pv7 oldhan7,t3.endTime eTime from (select date_format(start_time, '%Y-%m-%d') as day,sum(pv) as day_pv,date_format(end_time, '%Y-%m-%d') endTime from page_data group by day) t3 left join ( select date_add(t1.day,INTERVAL 1 DAY) as last_day,t1.day_pv as day_pv1 from (select date_format(start_time, '%Y-%m-%d') as day,sum(pv) as day_pv from page_data group by day) t1 ) t2 on t3.day=t2.last_day LEFT JOIN ( select date_add(t1.day7,INTERVAL 7 DAY) as last_day7,t1.day_pv7 as day_pv7 from (select date_format(start_time, '%Y-%m-%d') as day7,sum(pv) as day_pv7 from page_data group by day7) t1 ) t7 ON t3.day=t7.last_day7 ) t5 WHERE DATE_FORMAT(t5.date,'%Y-%m-%d') between '2020-06-01' and '2020-07-24' GROUP BY t5.date) tpv
返回结果