Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicate rows in SQL Server by multiple conditions

Tags:

sql

sql-server

My fields are

ID | Name | StartTime | EndTime | Date | Description

I am looking for a way to select all rows with the same entries in all fields except for the ID.

I am not that familiar with SQL so I tried this approach but there is only one field relevant not (as in my case) five.

My first idea was to try something like:

SELECT *
FROM Table
order by Name, Date, StartTime, EndTime, Description

if I would look through all entries I would at least find the duplicates but that is definitely not the best way to solve the problem.

like image 918
ruedi Avatar asked Jun 21 '26 21:06

ruedi


2 Answers

This should do what you need:

select Name, Date, StartTime, EndTime, Description
from   table
group by Name, Date, StartTime, EndTime, Description
having count(*) > 1
like image 181
Jens Avatar answered Jun 23 '26 11:06

Jens


This query should work for you:

SELECT ID, Name, StartTime, EndTime, Date, Description
FROM (
    SELECT 
        ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID, Name, Date, StartTime, EndTime, Description) AS 'IndexNr'
        , ID
        , Name
        , StartTime
        , EndTime
        , Date
        , Description
    FROM Table) AS ResultSet
WHERE ResultSet.IndexNr > 1
like image 44
Radu Gheorghiu Avatar answered Jun 23 '26 09:06

Radu Gheorghiu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!