Avatar of pclarke7
pclarke7

asked on 

INVALID SUM RESULTS from SQL JOIN

Hello,
I have two simple tables Header & Detail

Header
Code     Quantity
AAA        10
AAA        20
AAA        30

Detail  
Code     Quantity
AAA       5
AAA       5
AAA       10
AAA       10
AAA       10
AAA       15
AAA       5

I need a SQL statement which will  validate that the Total for Code AAA on the header table  matches the Total for Code AAA on the detail table

  SELECT a.code AS "Code",  SUM(a.quantity ) AS "Header Total", SUM(b.quantity) AS "Detail Total"
  FROM Header a, Detail b WHERE a.code=b.code
  GROUP BY a.code


However I am getting the following for AAA

Code   Header Total          Detail Total
AAA         420                          180


I understand that because it is not a 1 for 1 relationship that this is causing the sum to be aggregated. How can I amend the query to give me the result I require

Code   Header Total          Detail Total
AAA         60                          60
Oracle DatabaseSQL

Avatar of undefined
Last Comment
pclarke7

8/22/2022 - Mon