0

我想在 Access 2003 中创建一个查询,它结合了两个查询来查找结果之间的差异。

查询一:[全部]

SELECT mars.Name, mars.Location, mars.Business_Unit 
FROM mars 
GROUP BY mars.Name, mars.Location, mars.Business_Unit;

那返回

Name   Location   Business Unit
-----  ---------  -------------
John   Sydney     AU
Grace  Brisbane   AU
Lee    Melbourne  GU

查询 2:[not_zero]

SELECT mars.Name, mars.Location, mars.Business_Unit 
FROM mars 
WHERE ((mars.orc)<>0) 
GROUP BY mars.Name, mars.Location, mars.Business_Unit;

它返回:

Name   Location  Business Unit 
-----  --------  -------------
John   Sydney    AU
Grace  Brisbane  AU

我正在尝试创建的查询:

SELECT m.Name, m.Location, m.Business_Unit 
FROM 
    all AS m 
    LEFT JOIN 
    Not_Zero AS o 
        ON (m.Name=o.Name) 
            AND (m.Location=o.Location) 
            AND (m.Business_Unit=o.Business_Unit) 
GROUP BY m.Name, m.Location, m.Business_Unit;

我希望我的查询能够实现:

Name  Location   Business Unit 
----  ---------  -------------
Lee   Melbourne  GU

但相反,我得到与 [all] 查询相同的结果:

Name   Location   Business Unit
-----  ---------  -------------
John   Sydney     AU
Grace  Brisbane   AU
Lee    Melbourne  GU

好的,所以这个 [mars] 表是这样的:

Name   Location   Business Unit  ORC
-----  ---------  -------------  ---
John   Sydney     AU             0
Grace  Brisbane   AU             5
John   Sydney     AU             10 
Grace  Brisbane   AU             0
Lee    Melbourne  GU             0
Lee    Paris      EU             0

我想要实现的是一直获取具有 orc = 0 的名称,这就是我想要的原因,例如:

Name  Location   Business Unit  ORC
----  ---------  -------------  ---
Lee   Melbourne  GU             0
Lee   Paris      EU             0
4

1 回答 1

1

由于 [not_zero] 查询结果将始终是 [all] 查询结果的子集,因此两者之间的“差异”是 [all] 中未出现在 [not_zero] 中的行。以下查询找到它们:

SELECT all.*
FROM 
    all 
    LEFT JOIN 
    not_zero 
        ON (all.NAME=not_zero.NAME) 
            AND (all.LOCATION=not_zero.LOCATION) 
            AND (all.[BUSINESS UNIT]=not_zero.[BUSINESS UNIT])
WHERE 
    (not_zero.NAME Is Null) 
        AND (not_zero.LOCATION Is Null) 
        AND (not_zero.[BUSINESS UNIT] Is Null);
于 2013-10-18T14:07:56.907 回答