Solved

Transact SQL Combine Customer Records

Posted on 2011-09-20
3
239 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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