Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sum of a column upto the current row for each row

sum of a column upto the current row for each row

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorial
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    Hi All, How to get sum of a column upto the current row for each row into a different column in SQL Server.

    Thanks & Regards, Md. Abdul Aleem NIIT technologies

    L C B 3 Replies Last reply
    0
    • I indian143

      Hi All, How to get sum of a column upto the current row for each row into a different column in SQL Server.

      Thanks & Regards, Md. Abdul Aleem NIIT technologies

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi, There's multiple ways of calculating a running total. This article[^] shows three of those solutions.

      I are Troll :suss:

      1 Reply Last reply
      0
      • I indian143

        Hi All, How to get sum of a column upto the current row for each row into a different column in SQL Server.

        Thanks & Regards, Md. Abdul Aleem NIIT technologies

        C Offline
        C Offline
        CitrusTech
        wrote on last edited by
        #3

        Not sure how this differs from your earlier question http://www.codeproject.com/Messages/3527850/Complex-Query.aspx[^] You're looking for running totals.

        Data Quality Tools and Data Profiling software

        1 Reply Last reply
        0
        • I indian143

          Hi All, How to get sum of a column upto the current row for each row into a different column in SQL Server.

          Thanks & Regards, Md. Abdul Aleem NIIT technologies

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          Not a full solution, but a step towards it: Let us assume a table "Table1" with following columns and values:

          ID val total
          1 12 <NULL>
          2 23 <NULL>
          3 2 <NULL>
          4 14 <NULL>

          Now you can update the "total" column for each row with a query like the one below for ID=3:

          UPDATE table1
          SET total = tmp.result
          FROM (SELECT MAX(ID) AS maxid, SUM(val) AS result
          FROM Table1
          WHERE (ID <= 3)) TMP
          WHERE table1.id = tmp.maxid

          Next, replace "3" by a parameter and run the query for every ID value...

          M 1 Reply Last reply
          0
          • B Bernhard Hiller

            Not a full solution, but a step towards it: Let us assume a table "Table1" with following columns and values:

            ID val total
            1 12 <NULL>
            2 23 <NULL>
            3 2 <NULL>
            4 14 <NULL>

            Now you can update the "total" column for each row with a query like the one below for ID=3:

            UPDATE table1
            SET total = tmp.result
            FROM (SELECT MAX(ID) AS maxid, SUM(val) AS result
            FROM Table1
            WHERE (ID <= 3)) TMP
            WHERE table1.id = tmp.maxid

            Next, replace "3" by a parameter and run the query for every ID value...

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            bhiller wrote:

            run the query for every ID value...

            What with a cursor or a while loop, what happens if he has 100k IDs. This is a bad solution, using the running total tools will give a much better result.

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups