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. Rolling Sums

Rolling Sums

Scheduled Pinned Locked Moved Database
question
3 Posts 2 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.
  • S Offline
    S Offline
    StevenWalsh
    wrote on last edited by
    #1

    I have a table that looks like this:

    11
    28
    18
    .
    .
    .

    i'm trying to roll up the values so it would take the value and add it to the previous sum of all the rows above it

    11
    39
    57
    .
    .
    .

    I am having the hardest time, any ideas?

    Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. -Fred Brooks

    W S 2 Replies Last reply
    0
    • S StevenWalsh

      I have a table that looks like this:

      11
      28
      18
      .
      .
      .

      i'm trying to roll up the values so it would take the value and add it to the previous sum of all the rows above it

      11
      39
      57
      .
      .
      .

      I am having the hardest time, any ideas?

      Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. -Fred Brooks

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Not sure, but sounds like ROLLUP / GROUPING operations. Have a look at this, it might help you: GROUPING (Transact-SQL)[^]

      The need to optimize rises from a bad design. My articles[^]

      1 Reply Last reply
      0
      • S StevenWalsh

        I have a table that looks like this:

        11
        28
        18
        .
        .
        .

        i'm trying to roll up the values so it would take the value and add it to the previous sum of all the rows above it

        11
        39
        57
        .
        .
        .

        I am having the hardest time, any ideas?

        Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. -Fred Brooks

        S Offline
        S Offline
        StevenWalsh
        wrote on last edited by
        #3

        ahhh stupid me, forgot about fetch

        DECLARE SalesYearCursor CURSOR FOR
        SELECT * FROM #tmpSalesYear

        OPEN SalesYearCursor
        
        FETCH NEXT FROM SalesYearCursor
        INTO @Percent,@xxxxx,@xxxxx,@xxx
        
        WHILE @@FETCH\_STATUS = 0
        BEGIN
        	SET @Current = @percent + @previous
        	SET @previous = @Current
        
        	UPDATE #tmpSales SET Roll = @Current
        	WHERE Percentage = @percent
        	AND SalesYear = @year
        	
        	FETCH NEXT FROM SalesYearCursor
        	INTO @Percent,@xxxxx,@xxxxxx,@xxxxxx
        END
        
        CLOSE SalesYearCursor
        

        DEALLOCATE SalesYearCursor

        EDIT: removed names that identify business :)

        Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. -Fred Brooks

        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