Results 1 to 2 of 2

Thread: Join with Sum

  1. #1
    Join Date
    Dec 2003
    Posts
    2

    Unanswered: Join with Sum

    Hello, I've got a trouble on the simple DB2 operation. I have two tables:

    Code:
    Gift
    ===================
    id id_employee
    1  1
    2  2
    3  4
    
    Amount
    ===================
    id id_employee amount
    1  1                 10
    2  1                 20
    3  2                 15
    4  5                 45
    How can I get a LEFT JOIN with summing the amounts. I need something like:

    Code:
    Result
    ===================
    id id_employee amount
    1  1                 30
    2  2                 15
    3  4                 0                <- important
    I hope it is easily done. Thx!

  2. #2
    Join Date
    Aug 2003
    Location
    Massachusetts, USA
    Posts
    106

    Re: Join with Sum

    Here's a way:

    select g.id, g.employee, SUM(a.amount) amount
    from gift g
    left outer join amount a on g.employee = a.employee
    group by g.id, g.employee

    Originally posted by Reebo
    Hello, I've got a trouble on the simple DB2 operation. I have two tables:

    Code:
    Gift
    ===================
    id id_employee
    1  1
    2  2
    3  4
    
    Amount
    ===================
    id id_employee amount
    1  1                 10
    2  1                 20
    3  2                 15
    4  5                 45
    How can I get a LEFT JOIN with summing the amounts. I need something like:

    Code:
    Result
    ===================
    id id_employee amount
    1  1                 30
    2  2                 15
    3  4                 0                <- important
    I hope it is easily done. Thx!

Posting Permissions

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