【发布时间】:2020-08-20 14:40:40
【问题描述】:
我正在尝试计算价格的每日百分比变化,并根据价格类型反映之前的观察结果。
当我运行这个查询时,它似乎在计算百分比变化,但计算是从一种价格类型到另一种价格类型完成的。
我想通过 pricetypeID 计算价格的百分比变化。
您知道如何实现它吗?任何提示将不胜感激!
select priceTypeID, date, price,
if(@last_entry = 0, 0, round(((price - @last_entry) / @last_entry) * 100,2)) "percentageChange",
@last_entry := price
from
(select @last_entry := 0) x,
(select date, `t`.price, `t`.priceTypeID
from `t`
order by `t`.priceTypeID, `t`.date asc) y;
order by date ASC
示例数据在这里: https://www.dropbox.com/s/fq57pks2d28i1j4/example.csv?dl=0
【问题讨论】:
标签: mysql percentage price