MS-Access-从两个表中查询用户

时间:2018-07-20 13:58:08

标签: ms-access

我有一个名为所有用户的表,其中包含所有用户及其对系统的访问总和。还有另外两个表,我用它与所有用户一起制作了一个表,这些表基本上散布在两个表中。

我的所有用户表存在问题,因为它没有与用户相对应的名称列。

tbl_sbc

Users Name
Foo  John

用户JDF

Users Name
Bar  Andrew

所有用户

Users
Foo
Bar

我希望查询使所有用户表上的用户交叉匹配,并检索其他两个表之一中的用户名。

我尝试做

SELECT [Users], [Name] ,Sum([Access]) AS Total
FROM (Select [Users], [Name] , [Access] from tbl_sbc
Union 
Select [Users], [Name] , [Access] from [Users JDF])
GROUP BY [Users]
ORDER BY [users] DESC;

但是我得到Your query does not include the specified expression [Name] as part of an aggregate function 但如果没有[名称],效果很好

1 个答案:

答案 0 :(得分:1)

接受

 SELECT [Users], [Name] ,Sum([Access]) AS Total
 FROM (Select [Users], [Name] , [Access] from tbl_sbc
 Union 
 Select [Users], [Name] , [Access] from [Users JDF])
 GROUP BY [Users]
 ORDER BY [users] DESC;

然后变成这个

 SELECT [Users], [Name] ,Sum([Access]) AS Total
 FROM (Select [Users], [Name] , [Access] from tbl_sbc
 GROUP BY [Users], [Name]
 Union 
 Select [Users], [Name] , [Access] from [Users JDF])
 GROUP BY [Users]
 ORDER BY [users] DESC;