我有一个表,其中包含 createdate、a、b、c、d、e 字段。我想创建一个显示以下内容的结果集:
createdate,a,b,c,d,e, [在过去 10 分钟内创建的具有相同 b,c,d 的记录数
我有一个表,其中包含 createdate、a、b、c、d、e 字段。我想创建一个显示以下内容的结果集:
createdate,a,b,c,d,e, [在过去 10 分钟内创建的具有相同 b,c,d 的记录数
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)
.
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