SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 10-09-2006, 01:36 AM
Simon
 
Posts: n/a
Default SQL 92 dialect question:

Selecting Duplicate Entries in a table?


I have a table with about 300 entires in it and I am trying to create a query that only selects the 33 records in the table that are duplicated.

These are my 3 column headings

TOrder_NO
TPart_NO
Qty

In 33 records TOrder_NO and TPart_NO are the same how can i run a query to just select these 33 records.

Cheers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 10-09-2006, 04:50 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: Selecting Duplicate Entries in a table?

SELECT torder_no,tpart_no FROM orders GROUP BY torder_no,tpart_no HAVING count(*)>1;
Supposedly two records are equal if Record1.TOrder_NO = Record2.TOrder_NO AND Record1.TPart_NO = Record2.TPart_NO

How did you found you have 33 duplicated records?

Hope this helps!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 10-09-2006, 06:05 PM
Simon
 
Posts: n/a
Default SQL 92 answer. Re: Selecting Duplicate Entries in a table?

Cheers for the reply that works but I need the Qty to be listed in the results but it dosen't affect the duplication. So it should just publish the QTY results, but it shouldn't affect the query.

Cheers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #4 (permalink)  
Old 10-09-2006, 10:38 PM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default SQL 92 answer. Re: Selecting Duplicate Entries in a table?

Simon,

What about :

SELECT torder_no,tpart_no, Qty FROM orders GROUP BY torder_no,tpart_no HAVING count(*)>1;

You could also try:

SELECT t1.torder_no, t1.tpart_no, t1.Qty FROM orders AS t1, orders AS t2 WHERE t1.order_no = t2.order_no AND t1.id <> t2.id

I am assuming that you also have a primary key (id) in the orders table.

Once you delete all the duplicates you may want to put a unique constraint on the order_no column to avoid duplicates in future.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-10-2006, 03:23 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: Selecting Duplicate Entries in a table?

So if you have got the following data:
order_no part_no qty
1 1 20
1 1 18
what should be the correct output - two rows or one row; in case of one row, what would be the criteria for selecting the quantity e.g. random, lowest, highest, other?

Best Regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #6 (permalink)  
Old 10-12-2006, 12:16 AM
Simon
 
Posts: n/a
Default SQL 92 answer. Re: Selecting Duplicate Entries in a table?

Cheers for the help so far, this is where am up to. I can select the items that are duplicated but when i try to select all 3 fields from my table it dosen't work.

Here is my query

SELECT Torder_NO, TPart_NO
FROM dbo.TestingGSS
GROUP BY Torder_NO, TPart_NO
HAVING (COUNT(*) > 1)

But I have a column called TOrder_Qty that i want included in the results but it shouldn't affect the crietria of the query. So far I have tried this numerous ways and it dosen't work.


Cheers for any help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Moderation Tools:


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find duplicate entries in a table? ben SQL - Questions and Answers 6 04-01-2008 07:30 AM


All times are GMT. The time now is 09:21 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7