开发者

SQL Access help in sum from 2 different tables

开发者 https://www.devze.com 2023-03-18 03:50 出处:网络
i have these tables table 1 idprice 130 240 350 table 2 idprice 170 25 310 i want a query开发者_如何学编程 that would sum the the price value based on the ID

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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消