8

I need to delete all duplicate rows except the first occurrence of similar rows from a Table Log having the same customer_id and deactivation_date. How do I achieve that with minimum number of sql statements.

I Am using MS SQL Server 2008 express edition.

Thakur
  • 1,890
  • 5
  • 23
  • 33
Akhil K Nambiar
  • 3,835
  • 13
  • 47
  • 85

3 Answers3

16

Something like:

DELETE FROM Log WHERE LogId NOT IN 
 (SELECT Min(LogId) FROM Log GROUP BY customer_id, deactivation_date)
Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50
14

Or:

with cte as (
    select row_number() over (partition by customer_id, deactivation_date
    order by log_id) as rn
    from Log)
delete from cte
    where rn > 1;
MicSim
  • 26,265
  • 16
  • 90
  • 133
2

You are not giving us much to work on. But maybe something like this:

;WITH CTE
AS
(
    SELECT
        ROW_NUMBER() OVER(PARTITION BY [Log].customer_id ORDER BY 
                              deactivation_date) AS RowNbr,
        [Log].*
    FROM
        [Log]
)
DELETE FROM [Log]
WHERE EXISTS
    (
        SELECT
            NULL
        FROM
            CTE
        WHERE
            CTE.RowNbr>1
            AND CTE.log_id =[Log].log_id 
    )
Arion
  • 31,011
  • 10
  • 70
  • 88