在其他博客上看到,分享给大家!
1、问题
数据如下:
table 1
date1 price
2018/1/1 12
2018/1/2 12
2018/1/3 12
2018/1/4 12
2018/1/5 12
2018/1/6 12
2018/1/7 12
table 2
date2 factor
2018/1/3 0.2
2018/1/6 0.3
2、方法
逻辑:当date1 < date2,price * factor(factor为累乘,eg.date1=2018/1/1时,则为12 * 0.2 * 0.3)。
方法:对factor进行多次对数转换,将乘法转化为加法,即power(10, sum(log(10, factor)))
select
date1,
power(10, sum( if(date1<date2, log(10, factor), 0))) * avg(price) as newprice
from table1
left join table2 on 1=1
group by date1