2013-06-25 65 views
1

我有一个表的字段createdate,a,b,c,d,e。我想创建一个结果,显示了以下内容:显示sql记录集中的相似记录数

CREATEDATE,A,B,C,d,E,[数的在最后10分钟用同样的B,C,d创建的记录

+0

您使用的数据库是? (SQL Server,MySQL,...?) – Andomar

+0

从“now”开始,距离最后一次插入的值是相同的值(b,c,d)还是10分钟? –

回答

0
SELECT 
    createdate, 
    a, 
    b, 
    c, 
    d, 
    e, 
    duplicatesCount = 
    (
     SELECT count(*) as c 
     FROM Table as t2 
     WHERE DATEADD(minute, 10, t1.createdate) > GETDATE() 
      AND t2.b = t1.b AND t2.c = t1.c AND t2.d = t1.c 
    ) 
    FROM Table as t1 
+0

你是不是指最后的条件在't2.d = t1.d' –

0
select * 
,  (
     select count(*) 
     from YourTable yt2 
     where yt1.b = yt2.b 
       and yt1.c = yt2.c 
       and yt1.d = yt2.d 
       and yt2.createdate between 
        yt1.createdate - interval 10 minute 
        and yt1.createdate 
     ) as DuplicateCount 
from YourTable yt1 

yt1.createdate - interval 10 minute语法是针对MySQL的。对于SQL Server,请使用dateadd(minute, -10, yt1.createdate)