Solved

Transact SQL Combine Customer Records

Posted on 2011-09-20
3
233 Views
Last Modified: 2012-05-12
Can someone please helpme with a SQL Update Query?

Occasionally I get duplicate customer records because they have setup a second account with their work email and forgot that they had originally setup their account with their home email.

I want to write a query that will essentially combine the 2 records and then delete 1 of them.

So, for example, I want to take record id #202 and record id #561 and combine 3 fields: TotalSpent, TotalNumberof Orders, and logincount and write the totals into record id #561. Then, upon successful update, delete record id #202.

Any help will be appreciated.

David
0
Comment
Question by:DLockwood
  • 2
3 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 36571018
try this
;with 
cte1 as	
(
	select AccountNum, Email, Spent, Orders, logincount,
	  ROW_NUMBER() over (PARTITION by AccountNum order by ID desc) RN 
	from customer
),
cte2 as
	(
	select AccountNum, SUM(Spent) TotalSpent, SUM(Orders) TotalOrders, sum(logincount) logincount, max(Email) Email, COUNT(*) Num
	from customer
	group by AccountNum
	)

update cte1
set Spent = B.TotalSpent,
    Email = coalesce(Email, B.Email),
    Orders = B.TotalOrders,
    logincount = B.logincount
from cte2 B
inner join cte1 on cte1.AccountNum = B.AccountNum and B.Num > 1
where RN = 1

delete 
from cte1 
where rn > 1

Open in new window

0
 

Accepted Solution

by:
DLockwood earned 0 total points
ID: 36580460
I found the easy answer to this question somewhere else.

The easy answer is to set 4 local variables equal to the values in the table that I want to delete.

Then run a simple update query updating the values to the old value + the local variable. Then delete the old record.

Thanks for your help anyway.
0
 

Author Closing Comment

by:DLockwood
ID: 36708074
Found the easy answer myself.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now