Click here to Skip to main content
16,020,347 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all
I have a problem
I would like to work as in the picture below ...
in sql server Query.

Thanks for your help to me.



http://www14.0zz0.com/2014/05/16/09/845618102.jpg[

[Edit CHill60]
For those not willing, or unable to, follow the link provided the image essentially shows the following
RowNumber	Column1	Column2 (How calculated)
1		614	614	Fixed value
2		187-	427	= 614-187
3		5-	422	= 427-5
4		21-	401	= 422-21
5		8-	393	= 401-8
6		30-	363	= 393-30
In other words the OP wants to calculate a figure based on the previous row.

What I mean is:
This query returns 3 columns.


Column1 in row 1 is fixed value (614) and put it to the row 1 in column2 and then

Row 2 in column2 = 614 -187=427


Row 3 in column2 =427 -5=422
....


I'm sorry for my English simple.

Thanks for All
Posted
Updated 16-May-14 1:20am
v3
Comments
CHill60 16-May-14 6:22am    
What have you tried so far? Do you have to use this table schema? (it is awful and the data is inconsistent in pattern)
sYsOm 17-May-14 3:10am    
What I mean is:
This query returns 3 columns.


Column1 in row 1 is fixed value (614) and put it to the row 1 in column2 and then

Row 2 in column2 = 614 -187=427


Row 3 in column2 =427 -5=422
....


I'm sorry for my English simple.

Thank you
appoos 2-Jun-14 9:25am    
What is you table structure. Do you have only one field in your table?

1 solution

Test it:
SQL
DECLARE @tmp TABLE (RowNo INT, Col1 INT)

INSERT INTO @tmp (RowNo, Col1)
VALUES(1, 614), (2, -187), (3, -5),
(4, -21), (5, -8), (6, -30)

SELECT t1.RowNo, t1.Col1, SUM(t2.Col1) AS CurrValue
FROM @tmp AS t1 INNER JOIN @tmp AS t2 ON t1.RowNo >= t2.RowNo
GROUP BY t1.RowNo, t1.Col1
ORDER BY t1.RowNo



Result:
RowNo  Col1      CurrVal
1      614       614
2      -187      427
3      -5        422
4      -21       401
5      -8        393
6      -30       363
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900