Access SQL:如何查找日期之间的重复记录

Access SQL: How to find duplicate records between dates

    SN            DATE
===========    =========
    111         1/1/2014
    222         2/1/2014
    333         3/1/2014
    111         4/1/2014
    222         5/1/2014
    333         6/1/2015
    222         7/1/2015
    111         8/1/2015
    333         9/1/2015
    111         10/1/2015
    111         11/1/2015

我有一个包含 2 列(SNDATE)的 table。我想创建一个查询,它将在 1/1/201431/12/2014 之间找到重复的 SN。我想计算重复项并用 SNDATE 显示重复的每一行。

试试这个

SELECT SN , Count(SN) AS Dup
FROM [TableName]
WHERE DATE BETWEEN #2014-01-01# AND #2014-12-31#
GROUP By SN
HAVING Count(SN) >1

一种方法是使用 exists:

select t.*
from table as t
where date between #2014-01-01# and #2014-12-31# and
      exists (select 1
              from table as t2
              where date between #2014-01-01# and #2014-12-31# and
                    t2.sn = t.sn and t2.date <> t.date
             );

但是,这不会找到在同一日期有两条记录的 sn。为此,您可以这样做:

select t.*
from table as t
where t.sn in (select t2.sn
                from table as t2
                where date between #2014-01-01# and #2014-12-31#
                group by t2.sn
                having count(*) >= 2
               );

只过滤掉那些没有被骗的:

Select * From YourTable
Where 
    ([Date] Between #2014-01-01# And #2014-12-31#)
    And SN Not In 
    (Select T.SN From YourTable As T
     Where T.[Date] Between #2014-01-01# And #2014-12-31#
     Group By T.SN
     Having Count(*) = 1)