« Restoring Client Trust and Securing a Lifetime of Loyalty with Microsoft Dynamics CRM | Main | Customer Effective Tablet Solution Featured at Tower Group Financial Services Conference »

May 22, 2012

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54fb34b6f8833016305ae9834970d

Listed below are links to weblogs that reference Return All-but-One Duplicate Records in CRM using SQL:

Comments

Glenn

Hello,

I've been reading the blog for sometime and thought I'd provide some feedback. Although your query works, it unfortunately doesn't work for all scenarios. In our database we have some contacts with exactly the same createdon date (we have a few records like that!). Therefore they are excluded. To fix this, your query could be reworked to take advantage of the newer SQL feature of OVER clause. The same query with OVER clause that works with the same createdon date would be (which i think is clearer to read as well) :-

SELECT contactid ,
fullname ,
emailaddress1
FROM ( SELECT contactid ,
fullname ,
emailaddress1 ,
ROW_NUMBER() OVER ( PARTITION BY fullname, emailaddress1 ORDER BY modifiedon DESC ) AS mostrecentrownumber ,
COUNT(*) OVER ( PARTITION BY fullname, emailaddress1 ) AS duplicatecount
FROM dbo.FilteredContact
WHERE statecode = 0
AND emailaddress1 IS NOT NULL
) AS dt
WHERE duplicatecount > 1
AND mostrecentrownumber = 1
ORDER BY dt.fullname

Thanks Glenn

Scott Sewell

Thanks Glenn
I like it and will definitely try it out - (That's part of the fun of this work, there's always a newer/better way to solve the puzzles. - Thanks for the tip!)
Scott -

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.

Search The Blog

  • Search the Blog

Twitter Updates

    follow me on Twitter