sql server - SQL find duplicate records from past 7 days -


hi trying find duplicate webvisits within past 7 days, have built query taking long run. in optimizing query appreciated. finding duplicates using visitorguid.

  tooclose       ( select     a.visitid beforeid,     b.visitid afterid,     a.omniturecid [before om id],     b.omniturecid [after om id],     a.pubid [before pub id],     b.pubid [after pub id],     a.visitorguid [before guid],     b.visitorguid [after guid],     a.date [before date],     b.date [after date]     webvisits     inner join webvisits b on a.visitorguid = b.visitorguid                     , a.date < b.date                     , datediff(day, a.date, b.date) < 7  a.date >= '7/1/2015')  select *  tooclose   beforeid not in (select afterid tooclose) 

if understand question correctly, trying find duplicate webvisits within past 7 days. not sure qualifies duplicate webvisit, here attempt might work you:

;with q1 (     select a.visitorguid           ,a.date     webvisits     a.date >= dateadd(day, -7, cast(getdate() date))     ) ,q2     (select q1.visitorguid            ,count(*) rcount        q1       group q1.visitorguid      ) select q2.visitorguid q2  q2.rcount > 1 

sql fiddle demo

updated

;with q1 (     select a.visitorguid           ,a.date           ,a.omniturecid      webvisits     a.date >= dateadd(day, -7, cast(getdate() date))     ) ,q2     (select q1.visitorguid            ,count(*) rcount        q1       group q1.visitorguid       having count(*)> 1      )  select q1.visitorguid,        q1.omniturecid,        q1.date   q1 inner join q2 on q1.visitorguid = q2.visitorguid 

sql fiddle demo2


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -