SQL中的复杂和

时间:2013-02-20 02:34:44

标签: sql ms-access grouping aggregation

假设我有:

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视为累积频率。

1 个答案:

答案 0 :(得分: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