首页 > 数据库技术 > 详细

mysql求同比环比

时间:2021-03-27 22:29:28      阅读:45      评论:0      收藏:0      [点我收藏+]
-- 参考:SQL计算月环比、月同比_路易吃泡面的博客-CSDN博客 
--
mysql同比环比 drop table if EXISTS ordertable; create table ordertable -- 做业绩表-基础表 SELECT date_format(STR_TO_DATE(concat_ws(/,Cyear,Cmonth),%Y/%m),%Y-%m) as datetime_month ,STR_TO_DATE(concat_ws(/,Cyear,Cmonth,Cday),%Y/%m/%d) as datetime_day ,sum(jftotalcount) as orderpay from order_pivot_11 GROUP BY date_format(STR_TO_DATE(concat_ws(/,Cyear,Cmonth),%Y/%m),%Y-%m) ,STR_TO_DATE(concat_ws(/,Cyear,Cmonth,Cday),%Y/%m/%d) ; -- -- SELECT date_format(DATE_ADD(DATE_FORMAT(CONCAT(‘2020-03‘,‘-01‘),‘%Y-%m-%d‘),INTERVAL -1 month ),‘%Y-%m‘) -- 2020-02 -- 计算同比环比 -- 公司 SELECT a.d,a.orderpay_a,b.dd,b.datetime_month,b.orderpay_b ,(a.orderpay_a-b.orderpay_b)/b.orderpay_b as month_rate ,concat(left((a.orderpay_a-b.orderpay_b)/b.orderpay_b *100,5),%) as 月环比 from ( SELECT datetime_month as d ,sum(orderpay) as orderpay_a from ordertable GROUP BY datetime_month ) a left join ( SELECT date_format(DATE_ADD(DATE_FORMAT(CONCAT(datetime_month,-01),%Y-%m-%d),INTERVAL 1 month ),%Y-%m) as dd,datetime_month ,sum(orderpay) as orderpay_b from ordertable GROUP BY date_format(DATE_ADD(DATE_FORMAT(CONCAT(datetime_month,-01),%Y-%m-%d),INTERVAL 1 month ),%Y-%m) ,datetime_month ) b on a.d=b.dd

 

mysql求同比环比

原文:https://www.cnblogs.com/pingzizhuanshu/p/14586334.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!