Link to home
Start Free TrialLog in
Avatar of szx248
szx248

asked on

Purging duplicate records?

I have a table with three fields:

Item        varchar(4)
Customer  varchar(4)
price        smallmoney


sometimes there are more than one matching record. A matching record is when (item + customer) is the same.

Is there a way to eliminate all the duplicates besides for the last one. The last one would be the one that was added to the table last. This table is a temporary table with no indexes.
Avatar of Aneesh
Aneesh
Flag of Canada image

ALTER your temp table to add an identity column 'i'

delete from #tempTable
WHERE i NOT IN (SELECT MAX(i) FROM #tempTable GROUP BY (item + customer) )
Avatar of szx248
szx248

ASKER

Very neat!

Now do I give the temporary table an identity column?

this is what my temporary table looks like:
 CREATE TABLE #tempfile ( cus varchar(4), item varchar(4) , price smallmoney)
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of szx248

ASKER

awesome!