Removing duplicate rows (based on values from multiple columns) from SQL table

后端 未结 4 866
春和景丽
春和景丽 2020-12-03 10:14

I have following SQL table:

AR_Customer_ShipTo

+--------------+------------+-------------------+------------+
| ARDivisionNo | Custo         


        
相关标签:
4条回答
  • 2020-12-03 10:23

    Sample SQL FIDDLE

    1) Use CTE to get max ship code value record based on ARDivisionNo, CustomerNo for each Customers

    WITH cte AS (
      SELECT*, 
         row_number() OVER(PARTITION BY ARDivisionNo, CustomerNo ORDER BY ShipToCode desc) AS [rn]
      FROM t
    )
    Select * from cte WHERE [rn] = 1
    

    2) To Delete the record use Delete query instead of Select and change Where Clause to rn > 1. Sample SQL FIDDLE

    WITH cte AS (
      SELECT*, 
         row_number() OVER(PARTITION BY ARDivisionNo, CustomerNo ORDER BY ShipToCode desc) AS [rn]
      FROM t
    )
    Delete from cte WHERE [rn] > 1;
    
    select * from t;
    
    0 讨论(0)
  • 2020-12-03 10:31

    ROW_NUMBER() is great for this:

    ;WITH cte AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY ARDivisionNo,CustomerNo ORDER BY ShipToCode DESC) AS RN 
                  FROM AR_Customer_ShipTo
                  )
    SELECT * 
    FROM  cte
    WHERE RN = 1
    

    You mention removing the duplicates, if you want to DELETE you can simply:

    ;WITH cte AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY ARDivisionNo,CustomerNo ORDER BY ShipToCode DESC) AS RN 
                  FROM AR_Customer_ShipTo
                  )
    DELETE cte
    WHERE RN > 1
    

    The ROW_NUMBER() function assigns a number to each row. PARTITION BY is optional, but used to start the numbering over for each value in a given field or group of fields, ie: if you PARTITION BY Some_Date then for each unique date value the numbering would start over at 1. ORDER BY of course is used to define how the counting should go, and is required in the ROW_NUMBER() function.

    0 讨论(0)
  • 2020-12-03 10:39

    With row_number function:

    SELECT * FROM(
                  SELECT ARDivisionNo, CustomerNo, CustomerName, ShipToCode,
                  row_number() over(partition by CustomerNo order by ShipToCode desc) rn
                  FROM AR_Customer_ShipTo) t
    WHERE rn = 1
    
    0 讨论(0)
  • 2020-12-03 10:43

    You didn't specify the version of SQL Server, but ROW_NUMBER is probably supported:

    select *
    from
     (
      select ...
         ,row_number() 
          over (partition by ARDivisionNo, CustomerNo
                order by ShipToCode desc) as rn 
      from tab
     ) as dt
    where rn = 1
    
    0 讨论(0)
提交回复
热议问题