2017-06-12 48 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,所以請標記此答案爲接受。 –

相關問題