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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Group by and Sum ()

SQL Group by and Sum ()

Scheduled Pinned Locked Moved Database
database
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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi I have an Sql Table Like this MatTable type = 1 Is IN Qty ( Buy ) type = 0 Is Out Qty ( Sell )

    mat qty type


    mat1 10 0
    mat2 2 0
    mat3 10 0
    mat1 5 1
    mat2 5 1
    mat2 7 1
    mat4 4 0
    mat4 4 0
    mat4 8 1
    mat3 6 1
    mat1 10 0

    How I can Get a result Like

    mat Current qty


    mat1 15
    mat2 -10
    mat3 4
    mat4 0

    Thank you In Advance

    I know nothing , I know nothing ...

    S 1 Reply Last reply
    0
    • L Lost User

      Hi I have an Sql Table Like this MatTable type = 1 Is IN Qty ( Buy ) type = 0 Is Out Qty ( Sell )

      mat qty type


      mat1 10 0
      mat2 2 0
      mat3 10 0
      mat1 5 1
      mat2 5 1
      mat2 7 1
      mat4 4 0
      mat4 4 0
      mat4 8 1
      mat3 6 1
      mat1 10 0

      How I can Get a result Like

      mat Current qty


      mat1 15
      mat2 -10
      mat3 4
      mat4 0

      Thank you In Advance

      I know nothing , I know nothing ...

      S Offline
      S Offline
      Stryder_1
      wrote on last edited by
      #2

      Try: "Select mat, sum(case type when 0 then qty * -1 else qty) [Current qty] from MatTable group by mat" or if you don't like to use a case statement then - select mat, sum(qty) from ( (Select mat, sum(qty * -1) qty from MatTable group by mat where type = 0) t1 union (Select mat, sum(qty) qty from MatTable group by mat where type = 1) t2 ) group by mat I didn't check either of these for syntax so let me know if you have problems running these...

      L 1 Reply Last reply
      0
      • S Stryder_1

        Try: "Select mat, sum(case type when 0 then qty * -1 else qty) [Current qty] from MatTable group by mat" or if you don't like to use a case statement then - select mat, sum(qty) from ( (Select mat, sum(qty * -1) qty from MatTable group by mat where type = 0) t1 union (Select mat, sum(qty) qty from MatTable group by mat where type = 1) t2 ) group by mat I didn't check either of these for syntax so let me know if you have problems running these...

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

        Thank you so much , it;s worked like charming , However I added a little "END" word to the first statement , SELECT MAT, SUM(CASE TYPE WHEN 0 THEN Qty ELSE -Qty END) AS CurrentQty FROM MatTable GROUP BY Mat thank you , for you code , it's so simple and brilliant :)

        I know nothing , I know nothing ...

        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