MySQL语句查询本月,上月,上上月的价格数据和两条环比数据
最近在改一个sql语句,因为它无法查出上上月的价格数据和环比数据。
先看一下原先的sql语句:
`
- select ROUND((thismonth.price - lastmonth.price ) / lastmonth.price *
100 ,2) hbzz, thismonth.prod_year prod_year,
thismonth.prod_month prod_month , thismonth.id,
ROUND(thismonth.price , 2) ‘dyjg’, ROUND(lastmonth.price , 2)
‘syjg’, thismonth.type, thismonth.legenddata FROM
(select AVG(info_prod.price) as
price,info_prod.type,info_prod.legenddata,info_prod.prod_year,info_prod.prod_month,info_prod.id
from info_prod info_prod where info_prod.dr=0 and
info_prod.prod_time > DATE_SUB( CURDATE( ), INTERVAL 3 MONTH ) GROUP
BY
info_prod.type,info_prod.legenddata,info_prod.prod_year,info_prod.prod_month)
as thismonth, (select AVG(info_prod.price) as
price,info_prod.type,info_prod.legenddata,info_prod.prod_year,info_prod.prod_month,info_prod.id
from info_prod info_prod where info_prod.dr=0 and
info_prod.prod_time > DATE_SUB( CURDATE( ), INTERVAL 3 MONTH ) GROUP
BY
info_prod.type,info_prod.legenddata,info_prod.prod_year,info_prod.prod_month)
as lastmonth WHERE CASE WHEN
thismonth.prod_month > 1 THEN thismonth.prod_year =
lastmonth.prod_year AND thismonth.prod_month =
lastmonth.prod_month + 1 WHEN thismonth.prod_month = 1 THEN
thismonth.prod_year = lastmonth.prod_year + 1 AND
lastmonth.prod_month = 12 END GROUP BY thismonth.id `
查询结果如下图:
很明显只能查出两个月的价格数据和一个环比。首先要理解原有的sql语句是什么意思,然后才能在原有的基础上修改。
要知道select查询语句的固定模式,select后面跟的是要显示的字段,from后面从那个表查,where要查询的条件,group by根据什么分组,order by根据什么排序。理解了大致的框架。就看sql的关键字,DATE_SUB() 函数从日期减去指定的时间间隔。
语法:DATE_SUB(date,INTERVAL expr type),date 参数合法的日期表达式
expr参数是希望添加的时间间隔,type类型有很多:second,hour,day,week,month。CURDATE() 函数返回当前日期。
下列代码就是修复以后的代码:
`
- SELECT ROUND( ( AVG(thismonth.price) - AVG(lastmonth.price) ) /
AVG(lastmonth.price) * 100, 2 ) hbzz1, ROUND( ( AVG(lastmonth.price)- AVG(twomonth.price) ) / AVG(twomonth.price) * 100, 2 ) hbzz2, thismonth.prod_year prod_year, thismonth.prod_month prod_month,
thismonth.id, ROUND(AVG(thismonth.price),2)‘dyjg’,
ROUND(AVG(lastmonth.price),2)‘syjg’, ROUND(AVG(twomonth.price),2)
‘twjg’, thismonth.type, thismonth.legenddata FROM ( SELECT AVG(
info_prod.price ) AS price, info_prod.type, info_prod.legenddata,
info_prod.prod_year, info_prod.prod_month, info_prod.id FROM
info_prod info_prod WHERE info_prod.dr = 0 AND
info_prod.prod_time > DATE_SUB( CURDATE( ), INTERVAL 3 MONTH ) GROUP
BY info_prod.type, info_prod.legenddata, info_prod.prod_year,
info_prod.prod_month ) AS thismonth, ( SELECT AVG(
info_prod.price ) AS price, info_prod.type, info_prod.legenddata,
info_prod.prod_year, info_prod.prod_month, info_prod.id FROM
info_prod info_prod WHERE info_prod.dr = 0 AND
info_prod.prod_time > DATE_SUB( CURDATE( ), INTERVAL 3 MONTH ) GROUP
BY info_prod.type, info_prod.legenddata, info_prod.prod_year,
info_prod.prod_month ) AS lastmonth, ( SELECT AVG(
info_prod.price ) AS price, info_prod.type, info_prod.legenddata,
info_prod.prod_year, info_prod.prod_month, info_prod.id FROM
info_prod info_prod WHERE info_prod.dr = 0 AND
info_prod.prod_time > DATE_SUB( CURDATE( ), INTERVAL 3 MONTH ) GROUP
BY info_prod.type, info_prod.legenddata, info_prod.prod_year,
info_prod.prod_month ) AS twomonth WHERE CASE WHEN
lastmonth.prod_month>1 THEN thismonth.prod_year= lastmonth.prod_year
AND twomonth.prod_year=lastmonth.prod_year AND thismonth.prod_month
= lastmonth.prod_month + 1 AND twomonth.prod_month = lastmonth.prod_month -1 WHEN lastmonth.prod_month = 1 THEN
lastmonth.prod_year = twomonth.prod_year + 1 AND
thismonth.prod_year=lastmonth.prod_year AND
thismonth.prod_month=lastmonth.prod_month+1 AND twomonth.prod_month
= 12 END GROUP BY thismonth.id ORDER BY thismonth.type, thismonth.legenddata
- AVG(twomonth.price) ) / AVG(twomonth.price) * 100, 2 ) hbzz2, thismonth.prod_year prod_year, thismonth.prod_month prod_month,
`
运行成功后如下图:
这样就成功的查询出来了当月,上月,上上月的价格,和两个环比数据。
主要是修改了where里的条件:
一个bug改三天,要感谢朋友们的热情帮助,哈哈