我要计算我有下表的累计金额列的百分比

id  amount cumulative_amount
1   10000  10000
2   15000  25000
3    5000  30000
4   10000  40000

我想输出为

id  amount cumulative_amount  percentage
1   10000  10000              100%
2   15000  25000              166.67% (i.e cumulative_amount*100/amount)
3    5000  30000              600%
4   10000  40000              400%

那么如何在mysql中编写查询以实现上述输出

我在查询下面写了

SELECT amount,SUM(amount) OVER(ORDER BY id) AS cumulative_amount,
concat(round((cumulative_amount/amount * 100 ),2),'%') AS percentage
FROM mytable ORDER BY id DESC LIMIT 1;

但是我收到了mysql错误,指出字段列表中的未知列'cumulative_amount',所以我要如何实现输出 请帮我

提前致谢

分析解答

您可以在下面尝试-

SELECT amount,SUM(amount) OVER(ORDER BY id) AS cumulative_amount,
concat(round((SUM(amount) OVER(ORDER BY id))/amount * 100 ),2),'%') AS percentage
FROM mytable ORDER BY id DESC LIMIT 1