在sql

时间:2016-08-22 15:19:50

标签: sql duplicates records

我需要找到一个表中两个字段在指定日期范围内出现多次的所有记录。 我使用下面的内容使我能够显示重复项,但我需要在一段时间内指定,每当我尝试包含日期范围时,它开始显示所有记录而不仅仅是重复项

非常感谢任何帮助!

SELECT  
y.ID,y.site,y.machine
  FROM calls y
    INNER JOIN (SELECT
                    site,machine, COUNT(*) AS CountOf
                    FROM calls
                    GROUP BY site,machine
                    HAVING COUNT(*)>1
                ) dt ON y.site=dt.site and y.machine=dt.machine

2 个答案:

答案 0 :(得分:0)

您必须在两个sql语句(内部和外部)中将日期范围放在where / on条件下,以便获取您要查找的确切记录。这可以通过以下语句来完成:

SELECT  
y.ID,y.site,y.machine
  FROM calls y
    INNER JOIN (SELECT
                    site,machine, COUNT(*) AS CountOf
                    FROM calls
                    WHERE Date BETWEEN StartOfRange AND EndOfRange
                    GROUP BY site,machine
                    HAVING COUNT(*)>1
                ) dt ON y.site=dt.site and y.machine=dt.machine
WHERE
    y.Date BETWEEN StartOfRange AND EndOfRange

答案 1 :(得分:-1)

试试这个:

SELECT y.ID,
       y.site,
       y.machine
FROM   calls y
WHERE  y.date BETWEEN ... AND ...
  AND  EXISTS (SELECT x.ID
               FROM   calls x
               WHERE  x.site = y.site
                 AND  x.machine = y.machine
                 AND  x.date BETWEEN ... AND ...
                 AND  x.ID <> y.ID);