Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running totals in SQL Server

Status
Not open for further replies.

pfilipe

Programmer
Jul 17, 2000
1
PT
I NEED HELP!!!!<br><br>I would like to know how to run a query that returns running totals. Let us suppose I have a table with this 3 rows: <br><br>field1&nbsp;&nbsp;Field2<br>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>and I want this kind of result:<br><br>field1&nbsp;&nbsp;Field2<br>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>17&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>Can anybody help me????<br>
 
This is one way to do it that works efficiently (one read).<br>From Inside SQL Server 7.0, 950 pages of Microsoft propaganda and useful titbits of info.<br>DECLARE @run_total int<br>SELECT @run_total = 0<br>UPDATE Field1<br>&nbsp;&nbsp;SET @run_total = Field1 = @run_total + Field2<br>FROM YourTable<br> <p>Malcolm Wynden<br><a href=mailto:wynden@island.dot.net>wynden@island.dot.net</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top