[英]How to calculate time difference between current and previous row in MySQL
[英]How can I calculate percentage change between a previous row from current row in the same column in sql?
这是我的查询,但百分比变化列是错误的
select
year(ref_date), trade, trade_category, totalvalue,
round(((totalvalue) - (LAG(totalvalue) OVER(ORDER BY year(ref_date))*100/ totalvalue)),0) percent_change
from
data.services
where
trade_category != "Commercial services, total"
and
trade_category = "Financial services"
group by
year(ref_date)
order by
year(ref_date), totalvalue;
[下面是结果,但百分比变化的计算有效][1] [1]:https://i.stack.imgur.com/eoIcT.png
tghe window function 的形式必须保留你不能添加/total
到它
LAG (column) OVER (PARTITION BY col2 ORDER BY col3)
在手册中阅读更多关于 window function的信息
select
year(year_date), type, type_category, total,
round(((100.00 * total) - (LAG(total) OVER(ORDER BY year_date)/ total)),0) percent_change
from
kev.trade
where
type = "science"
and
type_category = "data services"
group by
year_date, type_category
order by
year_date, total;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.