查找Access 2003中两个查询的结果之间的差异

时间:2013-07-25 04:24:33

标签: sql ms-access ms-access-2003

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

查询1:[全部]

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

1 个答案:

答案 0 :(得分: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);