1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

Selecting Duplicate Entries in a table?

Discussion in 'SQL - Questions and Answers' started by Simon, Oct 9, 2006.

  1. Simon Guest

    Dialect: SQL 92
    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
  2. Dimitar Guest

    Dialect: SQL 92
    SQL:

    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!
  3. Simon Guest

    Dialect: SQL 92
    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
  4. ben Administrator

    Dialect: SQL 92
    Simon,

    What about :

    SQL:

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


    You could also try:

    SQL:

    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.
  5. Dimitar Guest

    Dialect: SQL 92
    So if you have got the following data:
    Code:
    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
  6. Simon Guest

    Dialect: SQL 92
    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

    SQL:
    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
  7. Unregistered Guest

    Simon,

    Try this

    SQL:
    SELECT COUNT(*) AS '# of Duplicate Entries',Torder_NO, TPart_NO,TOrder_Qty
    FROM dbo.TestingGSS
    GROUP BY Torder_NO, TPart_NO, TOrder_Qty
    HAVING (COUNT(*) > 1)


    This should work for you. Should show # of Duplicate Entries, Torder_NO, TPart_NO, TOrder_Qty

    Best Regards

Share This Page