473,625 Members | 3,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing duplicate records

24 New Member
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, 500 views)
Sep 28 '09 #1
7 3593
Delerna
1,134 Recognized Expert Top Contributor
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 New Member
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
Anthony97
24 New Member
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 Recognized Expert Top Contributor
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 TheOriginalTabl e and rename TheCopyTable
Or
Delete all records in TheOriginalTabl e 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 TheOriginalTabl e 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
kabaari
1 New Member
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 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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
3667
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 address should be in the table. Following http://www.sqlteam.com/item.asp?ItemID=3331 I have been able to get a duplicate count working: select Email, count(*) as UserCount
3
17939
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. Before doing that I would like to filter out duplicate AccountNumbers. I get most of the duplicates filtered out by using this query: select * from customers where AccountNumber NOT IN (select AccountNumber from customers where AccountNumber...
4
3614
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 the app has a mixture of select boxes, list boxes and checkboxes. The form submits the page to processAIMR.asp and then does the inserting. I am using a loop to insert a new record for each checkbox checked or listbox entry selected. My...
2
1477
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 duplicate transactions been entered, however they are unique because they have different primary key / ID. Does anyone know the best way to remove the duplicates from the table but
1
3701
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 dataTable,, Pls Suggest ASAP!!
16
4166
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 from within an SQL query - leaving one of the records behind of course. I have a mailing list comprised of a union query that gets records from two separate tables. I want to be able to run a query that removes one (or more) of the duplicated...
1
2257
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
4011
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 18-Apr-08 01-Mar-08 Fees: Asset 1 R 31,200.00 R 31,200.00 NMBM 22-Apr-08 23-Mar-08 P-MI (E) 07/2006 3 R 47,485 R 38,849 i have 200 records deplayed in the form i'm using access2007 and i have a command button which is the built in command button to...
0
1355
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 2001 2001
0
8192
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7188
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.