se还将问题中的标记从mysql更改为sql server。嗨,Kordirko,由于某种原因,“interval”给了我一个错误“无效列名”。我将您的“between”语句替换为“AND DateDIFF(day,b.creationdate,a.cre
se还将问题中的标记从mysql更改为sql server。嗨,Kordirko,由于某种原因,“interval”给了我一个错误“无效列名”。我将您的“between”语句替换为“AND DateDIFF(day,b.creationdate,a.cre,sql,sql-server-2008,tsql,compare,report,Sql,Sql Server 2008,Tsql,Compare,Report,se还将问题中的标记从mysql更改为sql server。嗨,Kordirko,由于某种原因,“interval”给了我一个错误“无效列名”。我将您的“between”语句替换为“AND DateDIFF(day,b.creationdate,a.creationdate)我在SQL Server 2008上在SQL Server上使用介于dateadd(day,-30,t2.creationdate)和dateadd(day,30,t2.creationdate)之间,我在回答中添加了一个带
se还将问题中的标记从
mysql
更改为sql server
。嗨,Kordirko,由于某种原因,“interval”给了我一个错误“无效列名”。我将您的“between”语句替换为“AND DateDIFF(day,b.creationdate,a.creationdate)我在SQL Server 2008上在SQL Server上使用介于dateadd(day,-30,t2.creationdate)和dateadd(day,30,t2.creationdate)之间
,我在回答中添加了一个带有SQL Server演示的查询。请将问题中的标签从mysql
更改为SQL Server
。
select accounttid, creationdate, personid from Table1 where personid in (
select (personid) from Table1 group by personid having COUNT (accountid) > 1)
EXAMPLE RESULT:
accountid creationdate personid
5501624 2013-05-01 101
5501544 2013-05-03 101
5510220 2013-10-24 10337
5504204 2013-06-27 10337
5502332 2013-05-21 1047
5502628 2013-05-28 1047
5508844 2013-10-01 1047
select distinct(personid, accountID)
from Table1
where Table1 personid in
(
select distinct(t1a.personid)
from Table1 as t1a
join Table1 as t1b
on t1a.personid = t1b.personid
and t1a.creationdate < t1b.creationdate
and datediff(dd, t1a.creationdate, t1b.creationdate) <= 30
)
{select accounttid, creationdate, personid
from Table1 where personid in (
select (personid)
from Table1
group by personid
having COUNT (accountid) > 1)} as a
join
{select accounttid, creationdate, personid from
Table1 where personid in (
select (personid) from
Table1
group by personid
having COUNT (accountid) > 1)} as b
on a.personid=b.personid and a.creationdate-b.creationdate >= 30
select accountid, creationdate, personid
from Table1 t1
where EXISTS(
SELECT 1 FROM Table1 t2
WHERE t1.personid = t2.personid
AND t1.accountid <> t2.accountid
AND t1.creationdate BETWEEN
t2.creationdate - interval 30 day
AND
t2.creationdate + interval 30 day
);
BETWEEN dateadd( day, -30, t2.creationdate )
AND
dateadd( day, 30, t2.creationdate )
select accountid, creationdate, personid
from Table1 t1
where EXISTS(
SELECT 1 FROM Table1 t2
WHERE t1.personid = t2.personid
AND t1.accountid <> t2.accountid
AND t1.creationdate BETWEEN
dateadd( day, -30, t2.creationdate )
AND
dateadd( day, 30, t2.creationdate )
);
select accountid, creationdate, personid
from Table1
where personid in (
select personid
from Table1
group by personid
having COUNT(accountid) > 1
);
select accountid, creationdate, personid
from Table1 t1
where EXISTS(
SELECT 1 FROM Table1 t2
WHERE t1.personid = t2.personid
AND t1.accountid <> t2.accountid
);