日期:2014-05-20  浏览次数:20962 次

sql语句 疑难问题求大仙帮忙
从数据看查询出有两列的数据 生成的表是第一列是从数据库取的数据 第二列是 第一列当列之前的和 
2    2
1    3
4    7
22   29

这个sql怎么写啊  救命啊
------最佳解决方案--------------------
加入每个记录有id,而且是顺序的


SELECT     f1,(SELECT     SUM(f1) AS total
                            FROM          test AS b
                            WHERE      (id < a.id)) AS Expr1
FROM         test AS a

------其他解决方案--------------------
with tb(id)
 as(
 select 2 union all
 select 1 union all
 select 4 union all
 select 22 
 ),
 source as(
 select id,rowindex=row_number()over(order by getdate()) from tb
 )
 select id,(select sum(id) from source source2 where source2.rowindex<=source1.rowindex)sumid from source source1
------其他解决方案--------------------
引用:
引用:加入每个记录有id,而且是顺序的



SQL code??



12345

SELECT     f1,(SELECT     SUM(f1) AS total                             FROM          test AS b                          ……

 WHERE      (id <=a.id)
------其他解决方案--------------------
貌似这个要用到递归
------其他解决方案--------------------
引用:
with tb(id)
 as(
 select 2 union all
 select 1 union all
 select 4 union all
 select 22 
 ),
 source as(
 select id,rowindex=row_number()over(order by getdate()) from tb
 )
 select id,(selec……
  
我上面的数字是 举个例子 ,我想做的是这种模式
------其他解决方案--------------------
引用:
引用:with tb(id)
 as(
 select 2 union all
 select 1 union all
 select 4 union all
 select 22 
 ),
 source as(
 select id,rowindex=row_number()over(order by getdate())……

我也只是举个例子
tb是表名,id是字段


select id,(select sum(id) from (select id,rowindex=row_number()over(order by getdate()) from tb)source2