probar el enfoque más simple con el CTE de SQL Server: http://www.sqlfiddle.com/#!3/2d386/2
datos:
CREATE TABLE tbl
([col1] int, [col2] int, [col3] int, [colX] varchar(1));
INSERT INTO tbl
([col1], [col2], [col3], [colX])
VALUES
(0, 1, 2, 'a'),
(0, 1, 2, 'b'),
(0, 1, 2, 'c'),
(0, 1, 2, 'a'),
(3, 4, 5, 'x'),
(3, 4, 5, 'y'),
(3, 4, 5, 'x'),
(3, 4, 5, 'z');
Solución:
select * from tbl;
with a as
(
select row_number() over(partition by col1 order by col2, col3, colX) as rn
from tbl
)
delete from a where rn > 1;
select * from tbl;
outpu t:
| COL1 | COL2 | COL3 | COLX |
-----------------------------
| 0 | 1 | 2 | a |
| 0 | 1 | 2 | b |
| 0 | 1 | 2 | c |
| 0 | 1 | 2 | a |
| 3 | 4 | 5 | x |
| 3 | 4 | 5 | y |
| 3 | 4 | 5 | x |
| 3 | 4 | 5 | z |
| COL1 | COL2 | COL3 | COLX |
-----------------------------
| 0 | 1 | 2 | a |
| 3 | 4 | 5 | x |
O tal vez esto: http://www.sqlfiddle.com/#!3/af826/1
datos:
CREATE TABLE tbl
([col1] int, [col2] int, [col3] int, [colX] varchar(1));
INSERT INTO tbl
([col1], [col2], [col3], [colX])
VALUES
(0, 1, 2, 'a'),
(0, 1, 2, 'b'),
(0, 1, 2, 'c'),
(0, 1, 2, 'a'),
(0, 1, 3, 'a'),
(3, 4, 5, 'x'),
(3, 4, 5, 'y'),
(3, 4, 5, 'x'),
(3, 4, 5, 'z');
Solución:
select * from tbl;
with a as
(
select row_number() over(partition by col1, col2, col3 order by colX) as rn
from tbl
)
delete from a where rn > 1;
select * from tbl;
Salida:
| COL1 | COL2 | COL3 | COLX |
-----------------------------
| 0 | 1 | 2 | a |
| 0 | 1 | 2 | b |
| 0 | 1 | 2 | c |
| 0 | 1 | 2 | a |
| 0 | 1 | 3 | a |
| 3 | 4 | 5 | x |
| 3 | 4 | 5 | y |
| 3 | 4 | 5 | x |
| 3 | 4 | 5 | z |
| COL1 | COL2 | COL3 | COLX |
-----------------------------
| 0 | 1 | 2 | a |
| 0 | 1 | 3 | a |
| 3 | 4 | 5 | x |
tablas de bases de datos no tienen un concepto de orden consecutivo. ¿Desea ordenar por min (colX) y mantener esas filas? ¿Hay una columna de marca de tiempo en la fila? –
¿Qué versión de SQL Server estás usando? Por lo que sé, no hay SQL Server 2010. –
Si tiene '0 | 1 | 3 | a' en sus datos, ¿debería mantenerse? o debería ser eliminado? –