Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Summation

  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Summation

    if i have a table like:
    id | btype | bnum
    1 | 0 | 10
    1 | 0 | 5
    1 | 10 | 4
    1 | 10 | 3

    is it possible to use a single SQL command to query the different of "bnum" base on "btype" of id "1". it should be (10+5)-(4+3) =8.

    if not, what is the smartest way to do this.

    thanks a lot for your kind help.
    Last edited by zawedx; 03-26-2009 at 11:10 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yeah... just group by the columns that you want to sum on. In your case, group by id, btype.

    Code:
    SELECT id, btype, SUM(bnum)
    FROM table1
    GROUP BY id, btype


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •