SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update using duplicate
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

satheesh
Posting Yak Master

United Kingdom
146 Posts

Posted - 07/23/2013 :  11:10:22  Show Profile  Reply with Quote
Hi,

I have field 'email' has many email address including duplicates

I can find duplicate and no of occurrence using

SELECT EmailAddress , count(*)
FROM renewal
GROUP BY EmailAddress
HAVING COUNT(EmailAddress + Lists ) > 1

However i need to update another column 'emailcount' for duplicate id like below

email emailcount
sg00sgt@gmail.com 1
sg00sgt@gmail.com 2
trs675t@yahoo.com 1
trs675t@yahoo.com 2
sg00sgt@gmail.com 3

How to modify the query to update the field.

Any help will be highly appreciated

Thanks

Regards,
SG

James K
Flowing Fount of Yak Knowledge

3578 Posts

Posted - 07/23/2013 :  11:30:42  Show Profile  Reply with Quote
;WITH cte AS
(
	SELECT 
		emailCount,
		ROW_NUMBER() OVER (PARTITION BY email ORDER BY email) AS NewemailCount
	FROM
		renewal
)
UPDATE cte SET emailCount = NewemailCount;
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22754 Posts

Posted - 07/28/2013 :  11:53:04  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
You may want to just use it in SELECT query so that you do not need to update the table whenever new email is added

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000