How to update running records in SQL Server -
Here below the table, I want to open the second row with the end of the first line and update it on so much. The closing column is calculated (opening + total)
ID total opening closing --------- ------------ - - ---------- 1 0 3015591.25 3015591.25 2 0 2146798.4 minus 3 0 3,015,591.25 minus 4 0 21 467 9 8.4 minus 5 0 3,015,591.25 minus 6 0 21 467 9 8.4 zero 7 0 3,015,591.25 zero 8 0 21467 9 8.4 Zero
and Output should be as follows:
Opening ID Total Closing --- - ------ -------- ---- ------------- 1 0 3015591,25 3015591,25 2 3015591,25 2146798,4 5162389,65 3 5162389.65 3015591.25 8177981.25 4 8177980.9 2146798.4 10324779.4 5 10324779.3 3015591.25 13340370.25 6 13340370.55 2146798.4 15487168.4 7 15487168.95 3015591.25 18502759.25 8 18502760,2 2146798,4 20649557,4
Any solution on this
WHILE
A related answer to the loop.
- Execute your table DECLARE @tbl TABLE (ID INT, float, total float, closing float) --Testing values insert in @tbl (ID, opening, total, closing) values (1,0,3015591.25,3015591.25) @tbl inserting values (id, opening, total, closing) values (2,0,2146798.4, zero) @tbl (id, opening, total, closing) values (3,0) , 3,015,591.25, zero) put @tbl (ID, opening, total closing) values (4, 0,2146798.4, zero) put @tbl (ID, opening, total closing) values (5,0,30l559lk25 zero ) @tbl insert ( ED, open, total, closing) values (6,02146798.4, zero) @tbl (ID, inserting in the opening, total, closing) values (7,0,3015591.25, zero) @tbl (ID, inserting in the opening, overall, closing) values (8,0,2l46798k4, zero) - - announced starting from dECLARE @StartCount INT, @TotalCount INT, @ select @OPENING float, @ off float @TotalCount = MAX (ID) TBL ; SET @StartCount = 2; While (@StartCount & lt; = @TotalCount) start selecting @OPENING = IsNull (closing, 0) Where ID select @tbl WHERE id = @StartCount - 1 of @CLOSING = @tbl (@OPENING + total) = @StartCount update @TBL SET opening = @OPENING, closing = @ clipping ID WH = id = @ Startkount sELECT @StartCount = @StartCount + select 1 eND * with @ ttbl
hope That it helps
Comments
Post a Comment