To find the rows you're looking for you need to do a self join on doc_nr
including only those rows where the descriptions do not match, SQL Fiddle.
CREATE TABLE basic
(
column_ref INT,
description VARCHAR(30),
dateField DATETIME,
amount DECIMAL(12,2),
doc_nr VARCHAR(30)
);
INSERT INTO basic (column_ref, description, dateField, amount, doc_nr)
VALUES (123, 'Cobra - Ni', '06/11/2015',505.50,'2000/10'),
(123, 'Cobra - Toung', '07/11/2015',505.50,'2000/12'),
(123, 'Cobra - Brain', '07/11/2015',505.50,'2000/25'),
(123, 'Cobra - Nisyor', '07/11/2015',505.50,'2000/10');
SELECT *
FROM basic b
JOIN basic q ON b.doc_nr = q.doc_nr
WHERE b.description != q.description
╔════════════╦════════════════╦════════════════════════╦════════╦═════════╦════════════╦════════════════╦════════════════════════╦════════╦═════════╗
║ column_ref ║ description ║ dateField ║ amount ║ doc_nr ║ column_ref ║ description ║ dateField ║ amount ║ doc_nr ║
╠════════════╬════════════════╬════════════════════════╬════════╬═════════╬════════════╬════════════════╬════════════════════════╬════════╬═════════╣
║ 123 ║ Cobra - Ni ║ June, 11 2015 00:00:00 ║ 505.5 ║ 2000/10 ║ 123 ║ Cobra - Nisyor ║ July, 11 2015 00:00:00 ║ 505.5 ║ 2000/10 ║
║ 123 ║ Cobra - Nisyor ║ July, 11 2015 00:00:00 ║ 505.5 ║ 2000/10 ║ 123 ║ Cobra - Ni ║ June, 11 2015 00:00:00 ║ 505.5 ║ 2000/10 ║
╚════════════╩════════════════╩════════════════════════╩════════╩═════════╩════════════╩════════════════╩════════════════════════╩════════╩═════════╝
In order to actually DELETE
the rows, replace the SELECT
statement above with the following (provided you want to DELETE
the rows which have the shorter description, your actual criteria may vary).
DELETE b
FROM basic b
JOIN basic q ON b.doc_nr = q.doc_nr
WHERE LEN(b.description) < LEN(q.description);
Credit for the above syntax here.