我可以使用 select 语句生成这样的表:
column1 total ------------- Item A 360 Item B 440 Item C 180
This is the SQL statement I use to create the above table:
select column1, sum( order_total) as total
from report
group by column1;
如何修改我的 SQL 语句以在其中添加第三列和第四列(显示总列的百分比)?百分比值基于第 1 列中的值。所以最后我尝试创建一个如下所示的表:
column1 total percentage_a percentage_b ---------------------------------------- Item A 360 108 252 Item B 440 220 220 Item C 180 70 110
因此,在上表中,percentage_a 和percentage_b 列占总列的一定百分比。如何修改我的 SQL 代码,以便项目 A 行将进行 30/70 拆分,项目 B 列将进行 50/50 拆分。
感谢您的帮助。我是新手。
最佳答案
类似这样的事情应该可以做到:
select
column1,
sum(order_total) as total,
sum(order_total) * 0.7 as percent_70_30,
sum(order_total) * 0.5 as percent_50_50
from
report
group by
column1;
关于mysql - 如何向包含前一列百分比的 select 语句添加额外列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44461116/