简体   繁体   English

访问在联合查询中删除重复项的某些部分

[英]Access Removing CERTAIN PARTS of Duplicates in Union Query

I'm working in Access 2007 and know nothing about SQL and very, very little VBA. 我在Access 2007中工作,对SQL和VBA知之甚少。 I am trying to do a union query to join two tables, and delete the duplicates. 我试图做一个联合查询来联接两个表,并删除重复项。

BUT, a lot of my duplicates have info in one entry that's not in the other. 但是,我的许多重复项在一个条目中有信息,而在另一个条目中则没有。 It's not a 100% exact duplicate. 这不是100%精确的重复项。

Example, 例,
Row 1: A, B, BLANK 第1行:A,B,空白
Row 2: A, BLANK, C 第2行:A,空白,C

I want it to MERGE both of these to end up as one row of A, B, C. 我希望将它们合并成A,B,C的一行。

I found a similar question on here but I don't understand the answer at all. 我在这里找到了类似的问题,但我根本不明白答案。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I would suggest a query like this: 我建议这样的查询:

select
  coalesce(t1.a, t2.a) as a,
  coalesce(t1.b, t2.b) as b,
  coalesce(t1.c, t2.c) as c
from
  table1 t1
  inner join table2 t2 on t1.key = t2.key

Here, I have used the keyword coalesce . 在这里,我使用了关键字coalesce This will take the first non null value in a list of values. 这将采用值列表中的第一个非null值。 Also note that I have used key to indicate the column that is the same between the two rows. 还要注意,我使用key来指示两行之间的列相同。 From your example it looks like A but I cannot be sure. 从您的示例来看,它看起来像A但是我不确定。

If your first table has all the key values, then you can do: 如果您的第一个表具有所有键值,则可以执行以下操作:

select t1.a, nz(t1.b, t2.b), nz(t1.c, t2.c) as c
from table1 as t1 left join
     table2 as t2
     on t1.a = t2.a;

If this isn't the case, you can use this rather arcane looking construct: 如果不是这种情况,则可以使用这种颇为神秘的构造:

select t1.a, nz(t1.b, t2.b), nz(t1.c, t2.c) as c
from table1 as t1 left join
     table2 as t2
     on t1.a = t2.a
union all
select t2.a, t2.b, t2.c
from table2 as t2
where not exists (select 1 from table1 as t1 where t1.key = t2.key)

The first part of the union gets the rows where there is a key value in the first table. 联合的第一部分获取第一张表中有键值的行。 The second gets the rows where the key value is in the second but not the first. 第二个获取键值位于第二个而不是第一个的行。

Note this is much harder in Access than in other (dare I say "real") databases. 请注意,这在Access中比在其他(我敢说是“真实的”)数据库中难得多。 MS Access doesn't support common table expressions (CTEs), union s in subqueries, or full outer join -- all of which would help simplify the query. MS Access不支持公用表表达式(CTE),子查询中的unionfull outer join -所有这些都将有助于简化查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM