I need your help building a sql query using vb6 and a access db. Here is the scenario: 2 Tables, Give and Have Tb1 fields Id, Name, Amount Tb2 Id, Name, Amount I need to have the total amount for each name in both tables so to have total Give column and total have column but my query doesn't function
Select tb1.id,tb1.name,sum(tb1.amount) as TG, tb2.id,tb2.name,sum(tb2.amount) as TH
from tb1 inner join
tb2
on tb1.id=tb2.id
group by... Etc
If i have 10 records where id = 1 on tb1 and 3 records on tb 2 the total amount on tb2 is wrong (it repeats the sum on tb2 for each record on tb1)
I have tried also using Union obtaining a correct result in row but i should want to obtain something like
Id Name Have Give
1 John Doe 200,00 76,00
I hope to explain better by pics
Triyng @Parfait suggest, the result obtained is very similar to the query I wrote previously.
Thanks in advance for your help
Consider joining aggregates of both tables separately by id
:
Aggregate Queries (save as stored Access queries)
SELECT tb1.idF
, tb1.[name]
, SUM(tb1.Give) AS TG
FROM tblGive tb1
GROUP BY tb1.idF
, tb1.[name]
SELECT tb2.IDB
, tb2.[name]
, SUM(tb2.Have) AS TH
FROM tblHave tb2
GROUP BY tb2.IDB
, tb2.name
Final Query (running Full Join Query to return all distinct names in either tables)
SELECT NZ(agg1.idF, agg2.idB) AS [id]
, NZ(agg1.name, agg2.name) AS [name]
, NZ(agg2.TH, 0) AS [Have]
, NZ(agg1.TG, 0) AS [Give]
FROM tblGiveAgg agg1
LEFT JOIN tblHaveAgg agg2
ON agg1.idF = agg2.idB
UNION
SELECT NZ(agg1.idF, agg2.idB) AS [id]
, NZ(agg1.name, agg2.name) AS [name]
, NZ(agg2.TH, 0) AS [Have]
, NZ(agg1.TG, 0) AS [Give]
FROM tblGiveAgg agg1
RIGHT JOIN tblHaveAgg agg2
ON agg1.idF = agg2.idB;
To demonstrate with below data
CREATE TABLE tblGive (
ID AUTOINCREMENT,
IdF INTEGER,
[Name] TEXT(10),
Give INTEGER
);
INSERT INTO tblGive (IdF, [Name], [Give]) VALUES (1, 'JOHN', 37);
INSERT INTO tblGive (IdF, [Name], [Give]) VALUES (2, 'ANNA', 10);
INSERT INTO tblGive (IdF, [Name], [Give]) VALUES (3, 'BILL', -37);
INSERT INTO tblGive (IdF, [Name], [Give]) VALUES (2, 'ANNA', 116);
INSERT INTO tblGive (IdF, [Name], [Give]) VALUES (1, 'JOHN', 120);
CREATE TABLE tblHave (
ID AUTOINCREMENT,
IDB INTEGER,
[Name] TEXT(10),
Have INTEGER
);
INSERT INTO tblHave (IDB, [Name], [Have]) VALUES (1, 'JOHN', 200);
INSERT INTO tblHave (IDB, [Name], [Have]) VALUES (2, 'ANNA', 400);
INSERT INTO tblHave (IDB, [Name], [Have]) VALUES (3, 'BILL', 150);
INSERT INTO tblHave (IDB, [Name], [Have]) VALUES (1, 'JOHN', 25);
INSERT INTO tblHave (IDB, [Name], [Have]) VALUES (1, 'JOHN', 70);
Final Full Join Query returns following result:
Hi @Parfait, thank you also for your answer. Your sql query gives a result, but it is different from my expectative, because it aggregate only the names in both tables. So if i have the scenario that i will illustrate below Tb1 1 John 150 2 Mike 300 1 John 200 Tb2 16 Tony 32 1 John 50 20 Anna 25 it gives only John with his correct amount in difference Give / Have, insteed i shoud want to find 1 John 350 50 2 Mike 300 0 20 Anna 0 25 Thanks
Thank you @Parfait, but i receive a syntax error in Join procedure. Obviously the two queries represented above work perfectly, it isn't so for the final query. i am still doing some testing to see if the error is caused by transcription errors
Thanks for your new answer, I had seen the forgetfulness and I had already corrected it in my test, the query works perfectly, unfortunately I have to tell you that even this formula does not produce the result I wanted. I get the names repeated in the same way as with the use of a normal INNER JOIN.
Thanks @Parfait, I have posted 3 explanatory pics ;-)
The third. First image is the table called Give, the second is the Have table. As you can see, in each table, you can find many indipendent movements. So, Bill have to receive money but he hasn't nothing to pay, others have records in both tables. The result could be the sinthesys of all movements in a column with an one row for each IDF