2017-06-12 72 views
-1

假设我们有一个table A从第一行添加列值到当前行

x y 

1 2 
2 3 
3 5 
4 6 
5 9 
6 10 

写这概括了像下面

table b 

x y 
1 2 
2 5 
3 10 
4 16 
5 25 
6 35 

上述问题是不使用待解决的查询循环。

+1

'mysql'或'SQL-Server'? – Jens

+1

您可否也请与我们分享您迄今尝试的查询? –

+0

我更感兴趣的是sql server –

回答

0
Set @count:=0; 
Select x,@count:[email protected]+y from tablename ORDER BY x; 

试试上面的查询。

希望这会帮助你。

+0

@Strawberry是的,我已经更新了我的答案。谢谢..!! _/\ _ –

2

用SUM()OVER()子句

CREATE TABLE #Table1 
    ([x] int, [y] int) 
; 

INSERT INTO #Table1 
    ([x], [y]) 
VALUES 
    (1, 2), 
    (2, 3), 
    (3, 5), 
    (4, 6), 
    (5, 9), 
    (6, 10) 
; 
; 
select x, sum(y) over (order by x) as y from #table1 
+0

谢谢Chanukya,这对我工作 –

+1

@ user3578800,所以请标记此答案为接受。 –

相关问题