1

假设我有:

A       B  
andy    10
andy    20
andy    30
andy    40
paul    10
paul    50
ryan    3

...我希望结果是:

A       B       C
andy    10      10
andy    20      30
andy    30      60
andy    40      100
paul    10      10
paul    50      60
ryan    3       3

什么查询将为我实现这一目标?将 C 视为累积频率。

4

1 回答 1

1

一种方法..未经测试的建议...子选择汇总了它们匹配的相关值并且在当前行之前按顺序排列...

SELECT
  name as A, 
  value as B,
  ( SELECT sum(value) FROM the_table t2 WHERE t2.name=t1.name 
    AND t2.second_order_field<=t1.second_order_field ) as C
FROM the_table t1
ORDER BY t1.name,t1.second_order_field
于 2013-02-20T10:02:15.227 回答