Ali
Ali

Reputation: 456

SQL Access help in sum from 2 different tables

i have these tables

table 1

id      price
1       30
2       40
3       50


table 2

    id      price
    1       70
    2       5
    3       10

i want a query that would sum the the price value based on the ID like if table1.id=table2.id then sum table1.price and table2.price the end result should be something like this

table 3

    id      price
    1       100
    2       45
    3       60

Upvotes: 0

Views: 3399

Answers (1)

Alex K.
Alex K.

Reputation: 175748

You can;

SELECT 
  TABLE1.ID, 
  TABLE1.PRICE+TABLE2.PRICE
FROM TABLE1 
  INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID;

Or if there are duplicate IDs in either table;

SELECT 
  TABLE1.ID, 
  SUM(TABLE1.PRICE+TABLE2.PRICE)
FROM TABLE1 
  INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
  GROUP BY TABLE1.ID;

Upvotes: 2

Related Questions