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. Complicated Summation Query Help

Complicated Summation Query Help

Scheduled Pinned Locked Moved Database
databasedata-structureshelpquestion
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.
  • J Offline
    J Offline
    JohnQuar1
    wrote on last edited by
    #1

    Hello, I have two columns, a category and a value. Below is the data:

    Category

    Total

    Devices

    38111

    Plant

    6063

    Improvements

    2266

    I want to add two more columns: Zero and Sum So the columns would be Category, Zero, Total, Sum Is there any way to take the addition of Zero and Total and have the result in Sum, THEN take the result of Sum, and make it the answer in the next row of Zero, then repeat by adding zero and Total? The reason why I want to do this is b/c I am trying to create a waterfall graph and this is the only way I can think to do it with the data that I have. Thanks for your time. JM I would be forever indebt to you if you could help out. Thanks!

    D 1 Reply Last reply
    0
    • J JohnQuar1

      Hello, I have two columns, a category and a value. Below is the data:

      Category

      Total

      Devices

      38111

      Plant

      6063

      Improvements

      2266

      I want to add two more columns: Zero and Sum So the columns would be Category, Zero, Total, Sum Is there any way to take the addition of Zero and Total and have the result in Sum, THEN take the result of Sum, and make it the answer in the next row of Zero, then repeat by adding zero and Total? The reason why I want to do this is b/c I am trying to create a waterfall graph and this is the only way I can think to do it with the data that I have. Thanks for your time. JM I would be forever indebt to you if you could help out. Thanks!

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      After thinking about this for a bit, I think I would do this by creating a TEMP table and cursor to loop through the main table. The TEMP table would be something like this:

      Category Zero Total Sum
      Devices 0 38111 38111
      Plan 38111 6063 44147
      Improvements 44174 2266 4640

      Use the following code snipet to get you going with the logic ...

      DECLARE myCURSOR CURSOR FOR
      SELECT CATEGORY, TOTAL
      FROM myTABLE

      OPEN myCURSOR
      FETCH NEXT FROM myCURSOR INTO @XCATEGORY, @XTOTAL

      SET @PBAL = 0.0

      WHILE(@@FETCH_STATUS=0) BEGIN

      SET @GTOTAL = @PBAL + @XTOTAL

      SET @MYSQL = 'INSERT INTO #MYTEMP'
      SET @MYSQL = @MYSQL + ' VALUES(' + CHAR(39) + @XCATEGORY+ CHAR(39) + ',' + @PBAL, + ',' + @XTOTAL + ','
      SET @MYSQL = @MYSQL + @GTOTAL + ')'

      PRINT @MYSQL

      EXEC(@MYSQL)

      FETCH NEXT FROM myCURSOR INTO @XCATEGORY, @XTOTAL

      END

      CLOSE myCURSOR
      DEALLOCATE myCURSOR

      GO

      Hope this helps you !:thumbsup:

      J 1 Reply Last reply
      0
      • D David Mujica

        After thinking about this for a bit, I think I would do this by creating a TEMP table and cursor to loop through the main table. The TEMP table would be something like this:

        Category Zero Total Sum
        Devices 0 38111 38111
        Plan 38111 6063 44147
        Improvements 44174 2266 4640

        Use the following code snipet to get you going with the logic ...

        DECLARE myCURSOR CURSOR FOR
        SELECT CATEGORY, TOTAL
        FROM myTABLE

        OPEN myCURSOR
        FETCH NEXT FROM myCURSOR INTO @XCATEGORY, @XTOTAL

        SET @PBAL = 0.0

        WHILE(@@FETCH_STATUS=0) BEGIN

        SET @GTOTAL = @PBAL + @XTOTAL

        SET @MYSQL = 'INSERT INTO #MYTEMP'
        SET @MYSQL = @MYSQL + ' VALUES(' + CHAR(39) + @XCATEGORY+ CHAR(39) + ',' + @PBAL, + ',' + @XTOTAL + ','
        SET @MYSQL = @MYSQL + @GTOTAL + ')'

        PRINT @MYSQL

        EXEC(@MYSQL)

        FETCH NEXT FROM myCURSOR INTO @XCATEGORY, @XTOTAL

        END

        CLOSE myCURSOR
        DEALLOCATE myCURSOR

        GO

        Hope this helps you !:thumbsup:

        J Offline
        J Offline
        JohnQuar1
        wrote on last edited by
        #3

        I will give that a shot, thank you for your help!

        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