Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove duplicates and update quantities

Status
Not open for further replies.

WelshyWizard

IS-IT--Management
Apr 23, 2006
89
GB
Hi all,

I've imported a csv file into SQL Server 2000 and it is riddled with duplicates. Each record has a quantity and what I want to do is remove the records that are duplicated and finally end up with one record which has an updated quantity.

So, for example, part P12 is found on 3 records, each as a quantity of 2. I want some SQL code to remove 2 of the records and update the quantity of the remaining record to be 6.

Any ideas?

Today is the tomorrow you worried about yesterday - and all is well.....
 
You could use your initial table as a staging table and then insert the grouped by records into the real table e.g.
Code:
DECLARE @TABLE1 TABLE (id int, description varchar(10), quantity int)
DECLARE @TABLE2 TABLE (id int IDENTITY(1,1), description varchar(10), quantity int)

INSERT @TABLE1 VALUES (1, 'Item 1', 2)
INSERT @TABLE1 VALUES (2, 'Item 1', 2)
INSERT @TABLE1 VALUES (3, 'Item 1', 2)
INSERT @TABLE1 VALUES (4, 'Item 2', 2)
INSERT @TABLE1 VALUES (5, 'Item 3', 2)

INSERT @TABLE2 
SELECT description, sum(quantity) as quantity
FROM @TABLE1
GROUP BY description

SELECT id, description, quantity 
FROM @TABLE2


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top