2016-08-22 77 views
0

我需要查找一個表中兩個字段在指定日期範圍內出現多次的所有記錄。 我已經使用了下面,它使我能夠顯示重複,但我需要在一段時間內指定,每當我嘗試包括一個日期範圍,它開始顯示所有記錄不只是重複的在sql中顯示重複的值

任何幫助將是多不勝感激!

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 
+1

我們展示的代碼,日期範圍 – montewhizdoh

+1

當然只是添加日期條件給你SELECT ... GROUP BY會給你想要的東西:這可以通過,語句如下進行。 – AntDC

回答

0

你必須把日期範圍,其中/在這兩個SQL語句,以獲得您正在尋找的確切記錄(內&外)的條件。

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 
+0

謝謝,這就是我錯過了,我只是把它添加到一個SQL語句! –

-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);