Search code examples
sqlgroup-byinner-join

How can I join two tables in SQL and use function SUM on one column and show two other columns?


num (Table1) and id (Table2) contain the same information, so I can join them. I can add the sum using a function in the select statement. However, I need help adding the "location" column to my output, which comes from Table2.

How do I take the sum of all the departments for each id, and also add a location column to the output?

What I'm looking to do:

Example tables and output

What I have so far:

Select num, sum(amt)
From Table1
Inner join Table2 On id = num
Group by num

Solution

  • Like this:

    Select num, location, sum(amt)
    From Table1
    Inner join Table2 ON id = num
    Group by num, location