Solved

Transact SQL Combine Customer Records

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server concatenate fields 10 38
How to use three values with DATEDIFF 3 35
table joins in qry 17 65
SQL Query--is not excluding a segment of my data 4 9
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…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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