MS-Access查询返回重复结果如何阻止此

时间:2018-04-11 20:00:41

标签: ms-access

我遇到了一个问题,我遇到了2个问题。首先是通过58000条记录非常慢。第二个是我在结果中获得了重复的AccountID。

对于重复的字段,我相信我错过了一个标准,但还没弄明白哪个标准。

如何更改查询以解决这两个问题?

输入:

Account startdate   enddate     AccountID
00001   5/1/2015    5/1/2017    63854
00001   5/1/2017    5/1/2020    73104
00002   4/1/2012    4/1/2014    23764
00002   4/1/2014    4/1/2017    54492
00003   12/1/2012   5/1/2014    43104
00003   5/1/2014    5/1/2015    59321
00003   5/1/2014    5/1/2015    59421
00004   10/1/2012   10/1/2014   42844
00004   10/1/2014   2/1/2017    55481
00004   10/1/2014   2/1/2017    55481
00006   10/1/2014   2/1/2017    55401
00006   10/1/2018   2/1/2019    55402

代码:

SELECT dups.account, 
   dups.startdate, 
   dups.enddate, 
   dups.accountid 
INTO   [renewal tags] 
FROM   dups, 
   dups AS Dups_1 
WHERE  ( ( ( dups.account ) = [dups_1].[account] ) 
     AND ( ( dups.startdate ) = [dups_1].[enddate] ) 
     AND ( ( dups.accountid ) <> [dups_1].[accountid] ) ) 
    OR ( ( ( dups.account ) = [dups_1].[account] ) 
         AND ( ( dups.enddate ) = [dups_1].[startdate] ) 
         AND ( ( dups.accountid ) <> [dups_1].[accountid] ) ); 

输出:

Account startdate   enddate AccountID
00001   5/1/2015    5/1/2017    63854
00001   5/1/2017    5/1/2020    73104
00002   4/1/2012    4/1/2014    23764
00002   4/1/2014    4/1/2017    54492
00003   12/1/2012   5/1/2014    43104
00003   5/1/2014    5/1/2015    59321
00003   5/1/2014    5/1/2015    59421
00004   10/1/2012   10/1/2014   42844
00004   10/1/2014   2/1/2017    55481
00004   10/1/2014   2/1/2017    55481

期望的输出:

Account startdate   enddate     AccountID
00001   5/1/2015    5/1/2017    63854
00001   5/1/2017    5/1/2020    73104
00002   4/1/2012    4/1/2014    23764
00002   4/1/2014    4/1/2017    54492
00003   12/1/2012   5/1/2014    43104
00003   5/1/2014    5/1/2015    59321
00003   5/1/2014    5/1/2015    59421
00004   10/1/2012   10/1/2014   42844
00004   10/1/2014   2/1/2017    55481

1 个答案:

答案 0 :(得分:0)

我应该使用内连接,并确保表中的帐户ID在连接条件中与自身不匹配。这解决了这两个问题。更正后的代码如下。

SELECT dups.accountid, 
   dups.accountnumber, 
   dups.startdate, 
   dups.enddate 
INTO   [renewal tags] 
FROM   dups 
   INNER JOIN dups AS Dups_1 
           ON dups.accountnumber = Dups_1.accountnumber 
              AND dups.accountid <> dups_1.accountid 
WHERE  (( ( dups.startdate ) = [dups_1].[enddate] )) 
    OR (( ( dups.enddate ) = [dups_1].[startdate] ));