sql server - Merge duplicate records and update its table -
can me problem sql query?
i want merge/(sum if necessary) data of customer duplicate customer mame.
in project, find out customer has been duplicated using code:
select firstname, lastname, count(1) repeatedcount customer group firstname, lastname having count(1) > 1 how can update customer table 1 customer record , sum of totalsales , totavisits in 1 record only.
sample data:
firstname lastname totalsales totalvisits ---------- ---------- -------------- ----------- michelle go 0.00 0 michelle go 6975.00 1 michelle go 1195.00 1 michelle go 9145.00 3 michelle go 57785.00 5 michelle go 5845.00 1 michelle go 0.00 0 michelle go 0.00 0 expected output:
firstname lastname totalsales tolalvisits ---------- ---------- -------------- ----------- michelle go 80945.00 11
you have use aggregate function sum group by.
query
select firstname,lastname, sum(totalsales) totalsales, sum(totalvisits) totalvisits customer group firstname,lastname; and better practice suggest add column customerid unique.
can group easily.
Comments
Post a Comment