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. SQL QUERY

SQL QUERY

Scheduled Pinned Locked Moved Database
databasehelpquestion
4 Posts 4 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.
  • D Offline
    D Offline
    Dobrobit
    wrote on last edited by
    #1

    I have a table Id , b1 Id B1 espectedresult 1 5 5 2 19 24 3 14 38 4 41 79 5 14 93 6 41 134 I want the sum b1 at every Id can some one help?

    D C Richard DeemingR 3 Replies Last reply
    0
    • D Dobrobit

      I have a table Id , b1 Id B1 espectedresult 1 5 5 2 19 24 3 14 38 4 41 79 5 14 93 6 41 134 I want the sum b1 at every Id can some one help?

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

      select sum(b1),id from tablename group by id

      1 Reply Last reply
      0
      • D Dobrobit

        I have a table Id , b1 Id B1 espectedresult 1 5 5 2 19 24 3 14 38 4 41 79 5 14 93 6 41 134 I want the sum b1 at every Id can some one help?

        C Offline
        C Offline
        Corporal Agarn
        wrote on last edited by
        #3

        It looks like what you want is a running total. I would have to do a search and do not have the time right now.

        1 Reply Last reply
        0
        • D Dobrobit

          I have a table Id , b1 Id B1 espectedresult 1 5 5 2 19 24 3 14 38 4 41 79 5 14 93 6 41 134 I want the sum b1 at every Id can some one help?

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Assuming MSSQL < 2012, something like this should work:

          SELECT
          ID,
          B1,
          (SELECT Sum(B1) FROM TheTable As T2 WHERE T2.ID <= T1.ID) As Actual
          FROM
          TheTable As T1
          ORDER BY
          ID

          http://www.sqlfiddle.com/#!3/59751/2[^] For MSSQL 2012:

          SELECT
          ID,
          B1,
          Sum(B1) OVER (ORDER BY ID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) As Actual
          FROM
          TheTable
          ORDER BY
          ID

          http://www.sqlfiddle.com/#!6/59751/1[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          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