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

===============>>#1 票数: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
               );

===============>>#2 票数: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

===============>>#3 票数: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)

  ask by John P translate from so

未解决问题?本站智能推荐: