简体   繁体   English

MS Access SQL 查询表并删除重复项

[英]MS Access SQL query a table and remove duplicates

I have reviewed several posts here and can't find the answer, but it may be that the problem and answer are a bit above my pay grade.我在这里查看了几个帖子,找不到答案,但可能是问题和答案有点高于我的薪水等级。

I have an MSAccess Table with two ID fields (ID1 and ID2), and many other columns.我有一个带有两个 ID 字段(ID1 和 ID2)和许多其他列的 MSAccess 表。

I want to query (SELECT statement) the table and need two things to happen.我想查询(SELECT 语句)表并需要发生两件事。 Not sure if the order matters.不确定订单是否重要。 I want the query to return all columns from the table.我希望查询返回表中的所有列。

  • No duplicates for ID1, and I simply don't care which record(s) gets thrown away. ID1 没有重复项,我根本不关心哪些记录被丢弃了。
  • No duplicates for ID2, and I want to keep the record where the Date field in the table is earlier than the other values for records with the same ID2 value. ID2 没有重复项,我想保留表中日期字段早于具有相同 ID2 值的记录的其他值的记录。

Just can't figure out how to do this with an SQL Select statement.只是无法弄清楚如何使用 SQL Select 语句执行此操作。

Example Data - assume DateFld is ASC from top to bottom示例数据 - 假设 DateFld 从上到下是 ASC

ID1    ID2    DateFld    ...
1      24
1      24
2      23
3      98
4      23
4      23
5      98
6      72

Keep rows 1, 3, 4, 8保留第 1、3、4、8 行

No indices, as this table was created from a make table query;没有索引,因为这个表是从 make table 查询创建的; but I can add them.但我可以添加它们。 Let me know if you need more.如果您需要更多,请告诉我。

For this sample data you can use NOT EXISTS :对于此示例数据,您可以使用NOT EXISTS

SELECT t.*
FROM tablename AS t
WHERE NOT EXISTS (
  SELECT 1 FROM tablename
  WHERE ID2 = t.ID2 AND DateFld < t.DateFld
)

Results:结果:

ID1 ID2 DateFld
1   24  ...
2   23  ...
3   98  ...
6   72  ...
SELECT
  ID1, 
  ID2, 
  [allOtherColumns]
FROM(
  SELECT 
      ID1, 
      ID2, 
      ROW_NUMBER() OVER (PARTITION BY ID1 ORDER BY ID1) AS ID1Selector, 
      ROW_NUMBER() OVER (PARTITION BY ID2 ORDER BY DateValue ASC) AS ID2Selector,
      [allOtherColumns)
  FROM tbl) AS InnerQry
WHERE ID1Selector=1 AND ID2Selector=1

Note, this is SQL Server code that might work in Access.请注意,这是可能在 Access 中工作的 SQL 服务器代码。 I don't have a lot of experience with Access, but I know SQL. In SQL, the ROW_NUMBER() window function will split ID1 by unique ID1s so the first ID1 it comes across will be the only one it selects, and the ID2Selector will give the Row Number 1 to the earliest date value it comes across within each individual ID2.我没有很多 Access 经验,但我知道 SQL。在 SQL 中,ROW_NUMBER() window function 会将 ID1 拆分为唯一的 ID1,因此它遇到的第一个 ID1 将是唯一选择的 ID1,而 ID2Selector会将行号 1 赋予它在每个单独的 ID2 中遇到的最早日期值。

There might be a more efficient way to do it, but that is how I always do it in SQL.可能有更有效的方法,但我在 SQL 中总是这样做。

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

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