MS Access匹配结构表第2部分中的行组

时间:2017-06-28 08:16:00

标签: sql ms-access

以下是: MS Access Match groups of rows in structure table

我的数据表如下:

GROUP   | SUB_GROUP  |  ParentID  |   ChildID  |      Name       | OWNER  | Assembly_Level | Area |   Position    | ITEM_CREATION
GROUP_1 | SubGroup_1 | Assembly_1 | Assembly_1 | Assembly_1_Name | User_1 | 0              | 0001 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_1 | Part_1     | Part_1_Name     | User_1 | 1              | 0001 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_1 | Part_2     | Part_2_Name     | User_1 | 1              | 0001 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_1 | Part_3     | Part_3_Name     | User_1 | 1              | 0001 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_2 | Assembly_2 | Assembly_2_Name | User_2 | 0              | 0001 | 0,0,0 0,0,0   |  01-01-2017  
GROUP_1 | SubGroup_1 | Assembly_2 | Part_1     | Part_1_Name     | User_2 | 1              | 0001 | 0,0,0 0,0,0   |  01-01-2017  
GROUP_1 | SubGroup_1 | Assembly_2 | Part_2     | Part_2_Name     | User_2 | 1              | 0001 | 0,0,0 0,0,0   |  01-01-2017  
GROUP_1 | SubGroup_1 | Assembly_2 | Part_3     | Part_3_Name     | User_2 | 1              | 0001 | 0,0,0 0,0,0   |  01-01-2017  
GROUP_1 | SubGroup_1 | Assembly_3 | Assembly_3 | Assembly_3_Name | User_3 | 0              | 0001 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_3 | Part_1     | Part_1_Name     | User_3 | 1              | 0001 | 500,0,0 0,0,0 |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_3 | Part_2     | Part_2_Name     | User_3 | 1              | 0001 | 500,0,0 0,0,0 |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_3 | Part_3     | Part_3_Name     | User_3 | 1              | 0001 | 500,0,0 0,0,0 |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_4 | Assembly_4 | Assembly_4_Name | User_4 | 0              | 0002 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_4 | Part_4     | Part_4_Name     | User_4 | 1              | 0002 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_4 | Part_5     | Part_5_Name     | User_4 | 1              | 0002 | 0,0,0 0,0,0   |  01-01-2016  
GROUP_1 | SubGroup_1 | Assembly_4 | Part_6     | Part_6_Name     | User_4 | 1              | 0002 | 0,0,0 0,0,0   |  01-01-2016

我在parfait的帮助下得到的查询:

SELECT DISTINCT
   a1.GROUP,
   a1.SUB_GROUP,
   a1.ParentID AS Assembly,
   (SELECT DISTINCT AssemblyTable.NAME
      FROM AssemblyTable
      WHERE AssemblyTable.ChildID=a1.ParentID) AS NAME,
   (SELECT DISTINCT AssemblyTable.OWNER
      FROM AssemblyTable
      WHERE AssemblyTable.ChildID=a1.ParentID) AS OWNER,
   a1.Area,
   (SELECT DISTINCT AssemblyTable.ITEM_CREATION
      FROM AssemblyTable
      WHERE AssemblyTable.ChildID=a1.ParentID) AS ITEM_CREATION,
   a2.ParentID AS CLONED_ASSEMBLY,
   (SELECT DISTINCT AssemblyTable.NAME
      FROM AssemblyTable
      WHERE AssemblyTable.ChildID=a2.ParentID) AS CLONE_NAME,
   (SELECT DISTINCT AssemblyTable.OWNER
      FROM AssemblyTable
      WHERE AssemblyTable.ChildID=a2.ParentID) AS CLONE_OWNER,
   (SELECT DISTINCT AssemblyTable.ITEM_CREATION
      FROM AssemblyTable
      WHERE AssemblyTable.ChildID=a2.ParentID) AS CLONE_CREATION
FROM (SELECT * FROM AssemblyTable ORDER BY AssemblyTable.ITEM_CREATION ASC) AS a1
   INNER JOIN (SELECT * FROM AssemblyTable ORDER BY AssemblyTable.ITEM_CREATION ASC) AS a2
      ON (a1.ParentID < a2.ParentID)
      AND (a1.Area = a2.Area)
      AND (a1.[Position] = a2.[Position])
      AND (a1.ChildID=a2.ChildID)
WHERE a1.ParentID<>a2.ParentID
ORDER BY a1.GROUP, a1.SUB_GROUP, a1.Area, a1.ParentID;

我偶尔会看到以下输出:

 Assembly  | Cloned Assembly | Area
Assembly_2 |    Assembly_1   | 0001

尽管我在加入和显示结果之前,之后和期间对数据进行了排序,但是在Assembly_2之前创建了Assembly_1,但是在它之后显示了它?关于如何强制左手装配列成为最早创建的项目的任何想法?

另外,我想要解决的另一个问题,以及只有大数据集才能发现的问题是,当你得到多个匹配时,匹配的程序集也会显示在列表中,即:

 Assembly  | Cloned Assembly | Area
Assembly_2 |    Assembly_1   | 0001
Assembly_2 |    Assembly_6   | 0001
Assembly_1 |    Assembly_6   | 0001

尽管这是一个完全合法的匹配,但它是不必要的,因为Assembly_1和Assembly_6都与Assembly_2相匹配。对于这个问题,我尝试查询查询,如下所示:

SELECT *
FROM Cloned_Query As a3
WHERE a3.Assembly NOT IN (SELECT DISTINCT a1.Assembly AS Assembly
   FROM Cloned_Query as a1
   INNER JOIN Cloned_Query as a2 ON a1.Assembly=a2.CLONED_ASSEMBLY);

但是这会返回“No Current Record”?!我的理论是获取在Assemble和CLONED_ASSEMBLY列/字段中找到的项目列表,并从Assembly列中删除它们。

再次,非常感谢任何帮助!

0 个答案:

没有答案