logo elektroda
logo elektroda
X
logo elektroda

[MS SQL] Finding and Displaying Duplicate Rows in Tables without Unique ID: Inverse of DISTINCT

aksimoN 21603 3
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 11465094
    aksimoN
    Level 10  
    I need something like the "inverse" of DISTINCT which removes duplicates from the select query. I need to display the same repeating lines

    Mom tables that look like this:

    Field1 Field2 Field3 Field4
    1 2 3 4
    2 2 3 5
    1 3 4 5
    3 4 4 2
    2 2 3 5

    As you can see, records 2 and 5 are identical, the query select distinct * from Table will return this record only once. What to write a query to return only records that repeat, i.e. records 2 and 5, or a query that will return only record 2 or 5

    I do not need to write that my table has 13,000 records and there is no unique ID, I know that about 35 different records are repeated for me (the difference between a regular select and the one with distinct) How can I find them ??
  • ADVERTISEMENT
  • #2 11465155
    marcinj12
    Level 40  
    Add Count (*) and GROUP BY - this should return two for duplicate records. Alternatively, you can select duplicate records immediately by adding HAVING Count (*)> 1 clause
  • ADVERTISEMENT
  • #3 11466757
    aksimoN
    Level 10  
    Well, that's what I meant (shock that I did not think about it myself, the more that I thought about count but did not group them in all the fields)

    This is also how to remove the takes without removing the originals and I can close the topic, because so far I can see it so that I delete everything and put these records again, but only once.
  • #4 11468319
    Dżyszla
    Level 42  
    With deletion it will be worse. In fact, if your idea doesn't bother you, it's good. Without an identifier, there is no chance to delete the indicated records, leaving one (unless the MS supports the LIMIT / TOP clause when deleting - then you could use this + stored procedure on the base).
ADVERTISEMENT