[英]Access Vb6 query
I need your help building a sql query using vb6 and a access db.我需要您帮助使用 vb6 和访问数据库构建 sql 查询。 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
这是场景: 2 个表,Give 和 Have Tb1 字段 Id,Name,Amount Tb2 Id,Name,Amount 我需要在两个表中都有每个名称的总金额,以便有总计 Give 列和总计有列,但我的查询不是 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)如果我有 10 条记录,其中 tb1 上的 id = 1 和 tb 2 上的 3 条记录,则 tb2 上的总金额是错误的(它为 tb1 上的每条记录重复 tb2 上的总和)
I have tried also using Union obtaining a correct result in row but i should want to obtain something like我也尝试过使用 Union 在行中获得正确的结果,但我应该想要获得类似的东西
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. Triyng @Parfait 建议,得到的结果与我之前写的查询非常相似。
Thanks in advance for your help在此先感谢您的帮助
Try using union all
and then aggregating:尝试使用
union all
然后聚合:
Select id, name, sum(tg) as tg, sum(th) as th
from (select id, name, amount as tg, 0 as th from tb1
union all
select id, name, 0, amount from tbl2
) as t
group by id, name;
I'm not sure if all versions of MS Access support union all
in the from
clause like that.我不确定是否所有版本的 MS Access 都支持这样的
from
子句中的union all
。 If not, that piece needs to be encapsulated in a view.如果没有,则需要将该部分封装在视图中。
Consider joining aggregates of both tables separately by id
:考虑通过
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:最终完全连接查询返回以下结果:
Using this DDL使用这个 DDL
CREATE TABLE tb1 (
ID AUTOINCREMENT,
IdF INTEGER,
[Name] TEXT(10),
Give INTEGER
);
INSERT INTO tb1 (IdF, [Name], [Give]) VALUES (1, 'JOHN', 37);
INSERT INTO tb1 (IdF, [Name], [Give]) VALUES (2, 'ANNA', 10);
INSERT INTO tb1 (IdF, [Name], [Give]) VALUES (3, 'BILL', -37);
INSERT INTO tb1 (IdF, [Name], [Give]) VALUES (2, 'ANNA', 116);
INSERT INTO tb1 (IdF, [Name], [Give]) VALUES (1, 'JOHN', 120);
CREATE TABLE tb2 (
ID AUTOINCREMENT,
IDB INTEGER,
[Name] TEXT(10),
Have INTEGER
);
INSERT INTO tb2 (IDB, [Name], [Have]) VALUES (1, 'JOHN', 200);
INSERT INTO tb2 (IDB, [Name], [Have]) VALUES (2, 'ANNA', 400);
INSERT INTO tb2 (IDB, [Name], [Have]) VALUES (3, 'BILL', 150);
INSERT INTO tb2 (IDB, [Name], [Have]) VALUES (1, 'JOHN', 25);
INSERT INTO tb2 (IDB, [Name], [Have]) VALUES (1, 'JOHN', 70);
This UNION ALL
query works此
UNION ALL
查询有效
SELECT Name
, SUM(Give) AS TotalGive
, SUM(Have) AS YotalHave
FROM (
SELECT Name, Give, 0 AS Have
FROM tb1
UNION ALL
SELECT Name, 0 AS Give, Have
FROM tb2
) AS t
GROUP BY Name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.