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

时间:2015-05-17 11:09:05

标签: sql ms-access duplicates

    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)的表格。我想创建一个查询,在SN1/1/2014之间找到重复的31/12/2014。我想计算重复项,并显示与SNDATE重复的每一行。

3 个答案:

答案 0 :(得分: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
               );

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

只过滤掉那些没有欺骗的人:

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)