如何将SQL中的累积公式从一列减到另一列

如何将SQL中的累积公式从一列减到另一列,sql,percentage,Sql,Percentage,我有以下数据: Contact Customers 1 2 2 3 3 1 4 6 使用包含客户总数的变量 此数据表示2名客户与customerservice联系过一次,3名客户与customerservice联系过两次等 我想有另一个专栏,显示客户联系customerservice x次或更多次的累计百分比。所以在这个例子中: Contact Customers Cumm% 1 2 100 2

我有以下数据:

Contact  Customers
1        2 
2        3 
3        1 
4        6 
使用包含客户总数的变量

此数据表示2名客户与customerservice联系过一次,3名客户与customerservice联系过两次等

我想有另一个专栏,显示客户联系customerservice x次或更多次的累计百分比。所以在这个例子中:

Contact  Customers Cumm%
1        2         100
2        3         83.3
3        1         58.3
4        6         50%
我尝试过几种方法,但我对SQL非常陌生,似乎找不到任何示例,需要一些帮助

如果你知道如何计算客户数量,那也可以,因为我知道如何计算百分比。我的意思是:

Contact  Customers CummCustomers
1        2         12
2        3         10
3        1         7
4        6         6

提前感谢

您可以通过子查询完成,如下所示:

SELECT
    Contact
,   Customers
,   (SELECT SUM(Customers) FROM myTable t2 WHERE t2.Contact >= t1.Contact) as CummCustomers
FROM myTable t1

.

您可以使用更快的分析查询来解决问题

select contact,customer,
sum(customer) over (order by contact desc) cumm_customer
from  table1 order by contact asc;
请参见此处的sqlfiddle演示

感谢您的回复,但不幸的是,我无法在sql语句中使用它。子查询返回了多个值。这是不允许的。。。等等,您正在使用哪个数据库管理系统?