Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Transact SQL Combine Customer Records

Posted on 2011-09-20
3
Medium Priority
?
256 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:Ephraim Wangoya
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

824 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