【问题标题】:MS Access Select query. show either a match or the not matchMS Access 选择查询。显示匹配或不匹配
【发布时间】:2020-02-22 05:39:33
【问题描述】:

我正在查询构建器中组合一个选择查询。

我有两张以客户 ID 连接的表,一张数据表和一张参考表。选择查询将根据存在的数据显示数据点和参考表中的一些数据。 例如:

Data table
Customer ID    Batch ID    Order Number    Address    Warehouse
1234           1111        6546            place1     Ware1     
1234           2222        6547            place1     Ware2 
1235           3333        6548            place2     Ware3 
1236           4444        6549            place2     Ware4 
1237           5555        6550            place2     Ware5 
1238           6666        6551            place2     Ware6

Reference table
Customer ID    CustomerName   Warehouse   Account
1234           Cust1                      151515 
1234           Cust1          Ware2       151516 
1235           Cust2                      454545 
1236           Cust3                      464646 
1237           Cust4                      474747 
1238           Cust5                      484848

我的限制是我无法访问所有不同客户仓库的综合列表来填写参考表,但由于大多数客户对他们的所有仓库使用相同的帐户,因此填写它们并不重要。那里然而,是一位对特定仓库拥有单独帐户的客户。我宁愿没有一个解决方案来迎合一个客户,而是可以与未来出现的任何拥有特定单独帐户的客户合作。

我希望创建的字段类型是,如果仓库匹配或不匹配(因为它是空白的),它将显示一个帐户。我一直在绞尽脑汁。我不确定是否有解决方案,但我想我会向社区提出。

编辑
如果您提供数据表并运行查询,这是我正在寻找的示例输出。我还用实际值更新了 account 列,这样就不那么混乱了

Query Results
CustomerName    Order Number    Account    Address
Cust1           6546            151515     Place1
Cust1           6547            161616     Place1
Cust2           6548            454545     Place2
Cust3           6549            464646     Place3
Cust4           6550            474747     Place4
Cust5           6551            484848     Place5

【问题讨论】:

  • 请向我们展示您对此样本数据的期望输出。
  • “Account1”、“Account2”字面上是字段中的实际值吗?这些是唯一可能的值吗?还是真的有唯一的帐号?
  • 是否有一个客户表,其中包含每个客户的唯一记录?如果是这样,可能不应该将 CustomerName 复制到 Reference 中。
  • 帐户是唯一的。我已经更新了表格以反映这一点。还提供了示例输出。

标签: sql ms-access select inner-join


【解决方案1】:

DLookup 域聚合函数可能会得到你想要的。考虑:

SELECT Data.CustomerID, Data.BatchID, Data.OrderNumber, Data.Address, Data.Warehouse, 
Nz(DLookUp("Account","Reference","CustomerID=" & [CustomerID] & " AND Warehouse='" & [Warehouse] & "'"), 
   DLookup("Account","Reference","CustomerID=" & [CustomerID] & " AND Nz([Warehouse])<>'" & [Warehouse] & "'")) AS Acct
FROM Data;

【讨论】:

    【解决方案2】:

    在存在匹配仓库时必须消除 Null 匹配之前,这非常简单。这是使用子查询消除仓库 Null 匹配的一种方法:

    SELECT tblData.[Customer ID], tblData.[Batch ID], tblData.[Order Number], tblData.Address, tblData.Warehouse, tblRef.Account
    FROM tblData LEFT JOIN tblRef ON tblData.[Customer ID]=tblRef.[Customer ID]
    WHERE (tblData.Warehouse=tblRef.[Warehouse] AND tblRef.Warehouse Is Not Null)
       OR (tblData.Warehouse Not In (SELECT tblRef.Warehouse FROM tblRef WHERE tblRef.Warehouse Is Not Null AND tblRef.[Customer ID] = tblData.[Customer ID]) AND tblRef.Warehouse Is Null);
    

    因此,仅对 [Customer ID] 进行左连接,包括仓库匹配且不为空的记录,还包括没有匹配仓库且另外不存在与给定 [Customer ID] 匹配的仓库的记录(这些在每个 [客户 ID] 的子查询中逐项列出)

    结果是:

    Customer ID    Batch ID    Order Number    Address    Warehouse    Account
    1234           1111        6546            place1     Ware1        Account1     
    1234           2222        6547            place1     Ware2        Account2 
    1235           3333        6548            place2     Ware3        Account1 
    1236           4444        6549            place2     Ware4        Account1 
    1237           5555        6550            place2     Ware5        Account1 
    1238           6666        6551            place2     Ware6        Account1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      相关资源
      最近更新 更多