473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

removing duplicate records

I'm working on removing duplicate records from a table in sql.
I've tried using select distinct, and I've grouped by but I'm still running into some issues. Attached is a sample of the output table.
Attached Files
File Type: txt example.txt (338 Bytes, 498 views)
Sep 28 '09 #1
7 3583
Delerna
1,134 Expert 1GB
and the query looks like?

Also, when you say you are trying to remove duplicate records from a table...
do you mean

1)you want to actually delete the extra records from the table?
Or do you mean
2) you want a query that dosn't display the duplicates but leaves them in the table?

Reason I ask is because your question suggests to me that you want the first option but you are using a query that does the second option and then wondering why the duplicates are still in the table
Sep 29 '09 #2
ssnaik84
149 100+
you mean duplicate columns are having same "Load Id" and "Shipment Id" values?

there is no other unique id to identify duplicates.. for delete operation..
Can you add an extra auto increment column into table, by which we can separate out duplicate records and delete them..
Sep 29 '09 #3
Delerna,

Yes I would like to delete the extra records from the table, that's exactly what I'm looking to do. Here's the code I tried to use but it removes too much, I found it while browsing the internet, it was borken down into 4 parts and does not do what I really want it to do.


--part 1

select * from tblChargeAmts


--part2

with temptable as
(
select ROW_NUMBER() over (PARTITION BY [Load Id], [Ship Id] ORDER BY [Pc Code]) As rownumber,*
FROM tblChargeAmts
)
Select * FROM temptable


--part 3

with temptable as
(
select ROW_NUMBER() over (PARTITION BY [Load Id], [Ship Id] ORDER BY [Pc Code]) As rownumber,*
FROM tblChargeAmts
)
delete from temptable where rownumber > 1

--part4

select * from tblChargeAmts
Sep 29 '09 #4
Delerna
1,134 Expert 1GB
Yes that query will completely remove all rows that are duplicated.
You don't want that, you want to leave just one of the records but remove the extra's, right.
You can't delete the duplicates manually because SQL Server won't let you.

There are many ways to achieve that.
1) add an auto number column to the table and either
write a query to delete each duplicate that has the highest auto number.
or

delete them manually

Once all duplicates are gone you can remove the new field

2) Create a copy of the table and delete all records from it(the copy).
Then do
Expand|Select|Wrap|Line Numbers
  1.     INSERT INTO TheCopyTable
  2.     SELECT distinct * from TheOriginalTable
  3.  
Then you could delete TheOriginalTable and rename TheCopyTable
Or
Delete all records in TheOriginalTable and reinsert them from the TheCopyTable and then delete TheCopyTable
Expand|Select|Wrap|Line Numbers
  1.     INSERT INTO TheCopyTable
  2.     SELECT distinct * from TheOriginalTable
  3.  
  4.     DELETE FROM TheOriginalTable
  5.  
  6.     INSERT INTO TheOriginalTable
  7.     SELECT * from TheCopyTable
  8.  
  9.     DROP TABLE TheCopyTable
  10.  
3) Similar to 2 but into a temporary table instead of a copy table
Then delete all records from TheOriginalTable and reinsert them back
from the temp table

4).... Use your imagination

5).... There are lots of possibilities

6) It all depends on your particular situation

If this is a one off task then I suggest that Option 2 may be easiest for you.


NOTE
You really should make "Load Id" and "Shipment Id" key fields so that
it will be impossible for users to create duplicate records in the future.
Sep 29 '09 #5
Delerna,

I have been trying to accomplish the same thing for a while, thanks. But, I import data from AS400 to SQL Server and my problem is importing duplicates and the fact that I have no PK in that table. I want to import only distinct values.

Thanks,

DJ Khalif
Oct 1 '09 #6
ck9663
2,878 Expert 2GB
You're going to have to import it first into a working table and run something to update your SQL Server main table.

Happy Coding!!!

--- CK
Oct 1 '09 #7
Delerna
1,134 Expert 1GB
I also, routinely do migration of data from an ERP software called M3 (or MoveX) on an AS400 to SQLServer via DTS.

I presume you are using DTS to import your data from an AS400 into SQLServer.
If that is the case I don't really understand the issue?
Why wouldn't you just do a

SELECT DISTINCT in the DTS query that is selecting the data on the AS400.
That should stop the duplicates getting into the SQLServer Table in the first place!

It also raises some questions.
In the DTS the query that is selecting data from the AS400.
Are you joining tables?
If so, do you not have enough condions in one of the WHERE clauses,
and that is why you are getting duplicate records imported to SQL server.


PS
In my posts, as with all experts here, I am just trying to be helpful.
Not knowing your experience level, I may say/ask something that you have tried/know already.
Oct 1 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Iain | last post by:
Hi I have inherited a web app with the following table structure, and need to produce a table without any duplicates. Email seems like the best unique identifier - so only one of each e-mail...
3
by: Rad | last post by:
I have a table . It has a nullable column called AccountNumber, which is of varchar type. The AccountNumber is alpha-numeric. I want to take data from this table and process it for my application....
4
by: Drew | last post by:
I have a permission tracking app that I am working on, and I have made the insert page for it. I am having issues on how to prevent duplicates from getting entered. Currently the interface for...
2
by: En | last post by:
Any ideas what to do for the best? I have a table that records some transaction data. Each entry in the table has an auto gen primary key / ID. Somewhere along the way there are 1000's of...
1
by: sumit | last post by:
Hi, During some processing, Records get inserted into dataTable,,now i want to remove duplicate records from the dataTable,,, how can it be achieved?? there is no primary key defined for...
16
by: tyrfboard | last post by:
I've been searching for awhile now on how to remove duplicates from a table within an Access db and have found plenty of articles on finding or deleting duplicates. All I want to do is remove them...
1
by: PerumalSamy | last post by:
Hi I am having table with more 13 lakhs records. I am having duplicate records in it. i need to remove that. I wrote the following query SELECT *
2
by: nomvula | last post by:
hi guys i need some help to duplicate records on my form datasheet: here's the example of my form results: ClientLookup DateCaptured ForecastDate Description ForecastQuantity Forecast Actual UJ...
0
by: Walter Thizo | last post by:
I have substring the duplicate different records from file and i want to load those records inside listbox or paradox table distinctly - This is records from file : Student No 2001...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.