I know that the answer to this is going to be extremely simple and I’ve solved this problem before but for some reason I’ve been stuck on this for an hour.
I have the following query:
SELECT table1.FIELD1, table2.sum(FIELD2), table3.sum(FIELD3)
FROM (table1 INNER JOIN table2 ON table1.JOBINSTID = table2.JOBINSTID)
INNER JOIN table3 ON table1.JOBINSTID = table3.JOBINSTID
GROUP BY FIELD1;
The results that it returns are totally bonkers. Instead of giving me the actual sum of Field2 it gives me a number WAYYY higher. I know why this is happening and somewhat understand it but I don’t know how to fix it. How do I change this query so that I get the correct sum?
EDIT: JOBINSTID is a unique ID in tables 1 and 2 but not in table 3.
If it’s only a many-to-one problem involving table1, you might want this:
If the problem is (also) between table2 and table3, you may need to rethink how your database is normalized, but you could try this, which I think will work in any case:
Given what you’ve said, it’s hard to tell what you want, because you haven’t described which relationships are 1-1 and which are many-1, or indicated which columns are unique in which tables.