Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Combining duplicate records

Author  Topic 

jmoponfire
Starting Member

3 Posts

Posted - 2005-07-27 : 21:15:54
I'm new to complex queries in a new job setting. Any help appreciated:

I'm working with a custom program that imports grocery store log files of different formats.

I'm importing a new format. I have all the records in a temp database, but I need to combine duplicate records.
For example, I have two records with the same UPC, same price, but different quantities:

UPC Price QTY
00000000001500000402|$0.445|135
00000000001500000402|$0.445|220

I have to write a query that finds all instances of duplicate UPC's, sums the QTY field, UPDATEs the QTY field of the first duplicate, and DELETEs the redundant records.

So far, I have figured out how extract the duplicate UPC's:

SELECT UPC_CODE AS dupUPCs
FROM CLT_SALES_IMPORT_F8BB87B8
GROUP BY UPC_CODE
HAVING (COUNT(*) > 1)

Now for each one of the results, I want to SUM the QTY field, update the QTY field of each first occurring duplicate record, and delete the remaining duplicates.

Can anyone help me out?

Thanks for looking.

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2005-07-27 : 21:29:22
Best thing to do is to SELECT into a temporary table, then truncate the source table and then restore the data.
Go to Top of Page
   

- Advertisement -