Sunday, February 19, 2012

Calculation including previous record value

Hi,
How can I do calculation on the SQL Server that would include previous
record.
e.g.
a,b field in the table
if a is first record, then a= b
else
a = a(previous record value)+ b
Thank you for help
Schapopa
*** Sent via Developersdex http://www.examnotes.net ***Hi
CREATE TABLE #Test (col1 INT NOT NULL PRIMARY KEY, col2 INT NOT NULL)
INSERT INTO #Test VALUES (1,100)
INSERT INTO #Test VALUES (2,500)
INSERT INTO #Test VALUES (3,800)
INSERT INTO #Test VALUES (4,900)
SELECT *,(SELECT SUM(col2) FROM #Test as q
WHERE m.col1 >= q.col1 )AS total
FROM #Test AS m
<schapopa> wrote in message news:u7kCv1lRGHA.336@.TK2MSFTNGP12.phx.gbl...
> Hi,
> How can I do calculation on the SQL Server that would include previous
> record.
> e.g.
> a,b field in the table
> if a is first record, then a= b
> else
> a = a(previous record value)+ b
> Thank you for help
> Schapopa
>
>
> *** Sent via Developersdex http://www.examnotes.net ***

No comments:

Post a Comment