[Solved] Delete duplicate rows using certain conditions in MS SQL Server

Introduction

When working with data in MS SQL Server, it is often necessary to delete duplicate rows based on certain conditions. This can be done using a combination of the SELECT DISTINCT and DELETE statements. The SELECT DISTINCT statement is used to return only unique rows from a table, while the DELETE statement is used to delete the duplicate rows. In this tutorial, we will discuss how to delete duplicate rows using certain conditions in MS SQL Server.

Solution

The following query can be used to delete duplicate rows using certain conditions in MS SQL Server:

DELETE FROM table_name
WHERE row_id NOT IN (SELECT MIN(row_id)
FROM table_name
GROUP BY column1, column2, column3);


Using the window lead() to get the next departure date, then we check date diff if less than 30 days then we remove the record :

with cte as (
  select *, 
      lead(stayid) over (partition by cid order by departure) as lead_stayid,
      DATEDIFF(day, departure, lead(departure) over (partition by cid order by departure)) as date_diff
  from mytable
)
delete from mytable
Where stayid in (
                select stayid
                from cte
                where date_diff is not null and date_diff < 30
                );

Demo here

Deleting Duplicate Rows Using Conditions in MS SQL Server

Duplicate rows can be a nuisance in any database, and Microsoft SQL Server is no exception. Fortunately, there are several ways to delete duplicate rows using conditions in MS SQL Server. In this article, we’ll discuss the different methods for deleting duplicate rows and provide examples of each.

Using the DELETE Statement

The most straightforward way to delete duplicate rows is to use the DELETE statement. This statement allows you to specify the conditions for which rows should be deleted. For example, if you wanted to delete all rows with the same value in a certain column, you could use the following statement:

DELETE FROM table_name
WHERE column_name = value;

This statement will delete all rows with the specified value in the specified column. You can also use the DELETE statement to delete rows based on multiple conditions. For example, if you wanted to delete all rows with the same value in two columns, you could use the following statement:

DELETE FROM table_name
WHERE column_name1 = value1
AND column_name2 = value2;

Using the DISTINCT Clause

Another way to delete duplicate rows is to use the DISTINCT clause. This clause allows you to select only distinct (unique) values from a table. For example, if you wanted to select only the distinct values from a certain column, you could use the following statement:

SELECT DISTINCT column_name
FROM table_name;

This statement will return only the distinct values from the specified column. You can also use the DISTINCT clause to select distinct values from multiple columns. For example, if you wanted to select only the distinct values from two columns, you could use the following statement:

SELECT DISTINCT column_name1, column_name2
FROM table_name;

Using the GROUP BY Clause

The GROUP BY clause is another way to delete duplicate rows. This clause allows you to group rows together based on a certain condition. For example, if you wanted to group rows together based on the value in a certain column, you could use the following statement:

SELECT column_name
FROM table_name
GROUP BY column_name;

This statement will group all rows with the same value in the specified column together. You can also use the GROUP BY clause to group rows together based on multiple conditions. For example, if you wanted to group rows together based on the value in two columns, you could use the following statement:

SELECT column_name1, column_name2
FROM table_name
GROUP BY column_name1, column_name2;

Conclusion

In this article, we discussed the different methods for deleting duplicate rows using conditions in MS SQL Server. We discussed the DELETE statement, the DISTINCT clause, and the GROUP BY clause. Each of these methods can be used to delete duplicate rows in MS SQL Server.