连接具有不同结构的两个表

时间:2019-11-02 06:17:34

标签: sql ms-access

我有两个在查询中定义有关系的表,我的目标是在不复制库存和差异信息的情况下合并这两个表。

表1中的库存信息不应该重复,因为它已经是汇总库存,而在表2中,由于我想查看技术人员的位置信息,因此需要按项目显示它。

如果表1中的项目在表2中没有记录(反之亦然),我仍然需要显示其详细信息。对于这种情况,我目前的解决方法是创建两个查询,然后在excel中导出输出,然后应用一些vlookup。

表1的详细信息:

+---------+-----------+----------+-----------+
| ITEMID  | WAREHOUSE | LOCATION | INVENTORY |
+---------+-----------+----------+-----------+
| ITEM001 | WHS001    | LOC001   | 5         |
| ITEM002 | WHS001    | LOC002   | 4         |
| ITEM003 | WHS001    | LOC003   | 2         |
+---------+-----------+----------+-----------+

表2的详细信息:

+---------+-----------+----------+--------------+-----------+
| ITEMID  | WAREHOUSE | LOCATION | LOCATIONNAME | INVENTORY |
+---------+-----------+----------+--------------+-----------+
| ITEM001 | WHS001    | T01      | TECH001      | 1         |
| ITEM001 | WHS001    | T02      | TECH002      | 2         |
| ITEM001 | WHS001    | T03      | TECH003      | 3         |
| ITEM002 | WHS001    | T01      | TECH001      | 1         |
| ITEM002 | WHS001    | T02      | TECH002      | 3         |
+---------+-----------+----------+--------------+-----------+

修订查询:

SELECT
  a.ITEMID, a.WAREHOUSE, a.LOCATION, a.INVENTORY,
  b.ITEMID, b.WAREHOUSE, b.LOCATION, b.LOCATIONNAME, b.INVENTORY,
  (a.INVENTORY-b.INVENTORY) as VARIANCE
FROM Table1  as a
INNER JOIN Table2 as b ON a.ITEMID = b.ITEMID
GROUP by
  a.ITEMID, a.WAREHOUSE, a.LOCATION,
  b.ITEMID, b.WAREHOUSE, b.LOCATION, b.LOCATIONNAME, a.inventory, b.inventory
order by a.ITEMID ASC

UNION ALL

SELECT
  NULL, NULL, NULL, null, 
  t.ITEMID, t.WAREHOUSE, t.LOCATION, t.LOCATIONNAME, t.inventory, NULL
from table2 t
where t.ITEMID > (select MIN (t.itemid) from table2 where  itemid = t.itemid );

输出:

+----------+-------------+------------+-------------+----------+-------------+------------+--------------+-------------+----------+
| a.ITEMID | a.WAREHOUSE | a.LOCATION | a.INVENTORY | b.ITEMID | b.WAREHOUSE | b.LOCATION | LOCATIONNAME | b.INVENTORY | VARIANCE |
+----------+-------------+------------+-------------+----------+-------------+------------+--------------+-------------+----------+
| ITEM001  | WHS001      | LOC001     | 5           | ITEM001  | KITBAG      | T01        | TECH001      | 1           | 4        |
| ITEM001  | WHS001      | LOC001     | 5           | ITEM001  | KITBAG      | T02        | TECH002      | 2           | 3        |
| ITEM001  | WHS001      | LOC001     | 5           | ITEM001  | KITBAG      | T03        | TECH003      | 3           | 2        |
| ITEM002  | WHS001      | LOC002     | 4           | ITEM002  | KITBAG      | T01        | TECH001      | 1           | 3        |
| ITEM002  | WHS001      | LOC002     | 4           | ITEM002  | KITBAG      | T02        | TECH002      | 3           | 1        |
+----------+-------------+------------+-------------+----------+-------------+------------+--------------+-------------+----------+

期望输出:

+----------+-------------+------------+-------------+----------+-------------+------------+--------------+-------------+----------+
| a.ITEMID | a.WAREHOUSE | a.LOCATION | a.INVENTORY | b.ITEMID | b.WAREHOUSE | b.LOCATION | LOCATIONNAME | b.INVENTORY | VARIANCE |
+----------+-------------+------------+-------------+----------+-------------+------------+--------------+-------------+----------+
| ITEM001  | WHS001      | LOC001     | 5           | ITEM001  | WHS001      | T01        | TECH001      | 1           | -1       |
| ITEM001  | WHS001      | LOC001     | 0           | ITEM001  | WHS001      | T02        | TECH002      | 2           | 0        |
| ITEM001  | WHS001      | LOC001     | 0           | ITEM001  | WHS001      | T03        | TECH003      | 3           | 0        |
| ITEM002  | WHS001      | LOC002     | 4           | ITEM002  | WHS001      | T01        | TECH001      | 1           | 0        |
| ITEM002  | WHS001      | LOC002     | 0           | ITEM002  | WHS001      | T02        | TECH002      | 3           | 0        |
| ITEM003  | WHS001      | LOC003     | 2           | NULL     | NULL        | NULL       | NULL         | 0           | 2        |
+----------+-------------+------------+-------------+----------+-------------+------------+--------------+-------------+----------+

2 个答案:

答案 0 :(得分:0)

欢迎您! 您应该使用UNION而不是UNION ALL

请看一下this question,其中包含差异的完整说明。

答案 1 :(得分:0)

即使表2中没有匹配项,您也要显示表1中的行。这是通过外部联接完成的:

buffer

我希望MS Access在语法上接受这一点。当涉及到SQL标准合规性时,MS Access简直太可怕了。

两个表中都有该物料的仓库。也许您想将其包括在arrays[0]子句中,也许不是。您决定:-)