Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2008
    Posts
    55

    Unanswered: Using Subtract & SUM in the Select Statement

    Hello All,

    I am trying to use addition and subtraction in a select statement. The query below does not work. Does anyone have any suggestions?

    Thanks in advance!


    select a.legcramt-sum(b.paidamt) as balance
    from tb945 a,
    tb949 b
    where a.eid = b.eid

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    To use aggregate functions by group you must have a GROUP BY clause.

    Cab you describe in words what you want to achieve - e.g. for each customer I wish to take the sum of their loans and take it away from their account balance.

    Also, providing your table structure is immensely helpful to provide a meaningful solution.
    George
    Home | Blog

  3. #3
    Join Date
    May 2004
    Location
    Seattle
    Posts
    1,313
    here's what I want to do:

    for each customer I wish to take their account balance and add it to my account balance. then take it away from their account balance.

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    working with Navision lately?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by jezemine
    for each customer I wish to take their account balance and add it to my account balance. then take it away from their account balance.
    that's a sweet trick

    how long before you get caught?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  6. #6
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by georgev
    To use aggregate functions by group you must have a GROUP BY clause.

    Cab you describe in words what you want to achieve - e.g. for each customer I wish to take the sum of their loans and take it away from their account balance.

    Also, providing your table structure is immensely helpful to provide a meaningful solution.

    I added GROUP BY a.legcramt and it worked.
    Each legcramt ( from table a) could have multiple paidamt's (from table b). I wanted to find the difference between the legcramt and the sum of all of the paidamt's.

    Thank you !

  7. #7
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    wouldn't you only want to take the 1/2 pennies?
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

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