按值选择记录值作为每个组的总记录值的百分比

时间:2021-07-20 12:49:38

I have a table like the one below

我有一张如下表所示的表格

product....group....sales
1..........A........1000
2..........A........2000
3..........B........3000

I would like to calculate the % of each group in total sales

我想计算每个组在总销售额中的百分比

A....50%
B....50%

Thanks in advance for any help!!!

在此先感谢任何帮助!

1 个解决方案

#1


Here is one method:

这是一种方法:

select groupid, sum(sales),
       100 * sum(sales) / (select sum(sales) from table)
from table
group by groupid;

If sales is an integer, you may need to convert it to a floating point representation, if you want more precision.

如果sales是一个整数,如果你想要更高的精度,你可能需要将它转换为浮点表示。

#1


Here is one method:

这是一种方法:

select groupid, sum(sales),
       100 * sum(sales) / (select sum(sales) from table)
from table
group by groupid;

If sales is an integer, you may need to convert it to a floating point representation, if you want more precision.

如果sales是一个整数,如果你想要更高的精度,你可能需要将它转换为浮点表示。