显示sql记录集中类似记录的数量

时间:2013-06-25 12:47:56

标签: sql duplicates

我有一个表创建了一个字段,a,b,c,d,e。我想创建一个显示以下内容的结果集:

createdate,a,b,c,d,e,[在过去10分钟内使用相同的b,c,d创建的记录数

2 个答案:

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

答案 1 :(得分: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