我写数据集求同比 select T1.orderTime, t1.PROVINCE, t1.CITY, t1.num 本期订单量, t2.num 上期订单量, round(t1.num/t2.num-1,4) 同比 from (select * from ( select cast(orderTime as date)orderTime, province, city, num from ( select concat('20',from_unixtime(o.created , '%y-%m-%d')) as orderTime,o.province,o.city,count(o.city) num from `order` o join shop sh on o.shop_id=sh.id where 1=1 /*and o.status=99 订单完成*/ and pay_status=2 /*支付成功*/ group by orderTime,o.province,o.city having num>0 )aa )bb where orderTime between '${tstart1}' and '${tend1}' )t1 left join (select * from ( select cast(orderTime as date)orderTime, province, city, num from ( select concat('20',from_unixtime(o.created , '%y-%m-%d')) as orderTime,o.province,o.city,count(o.city) num from `order` o join shop sh on o.shop_id=sh.id where 1=1 /*and o.status=99 订单完成*/ and pay_status=2 /*支付成功*/ group by orderTime,o.province,o.city having num>0 )aa )bb where orderTime between '${YEARDELTA(tstart1,-1)}' and '${YEARDELTA(tend1,-1)}' )t2 on t1.PROVINCE=t2.PROVINCE and t1.CITY=t2.CITY 正确的数据应该是这样的: 左连接以后 数据变成了这样 |