I have following SQL table:
AR_Customer_ShipTo
+--------------+------------+-------------------+------------+
| ARDivisionNo | Custo
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;