将特定值与同一表中的其他行进行比较后删除一行

时间:2017-07-21 13:15:18

标签: sql sql-server

我有一个这样的表(SQL SERVER)

col1    col2    col3    col4    col5
1       Goerge   A       B       ++
2       Alex     B       B       aa
2       Alex     B       B       ++

现在第二行和第三行的值几乎相同,但col5除外。在这种情况下,我想删除其中一个(我希望能够决定从他们两个我想删除的行)像有价值++的那样,所以表应该是这样的:

col1    col2    col3    col4    col5
1       Goerge   A       B       ++
2       Alex     B       B       aa

任何人都可以帮助我:)

3 个答案:

答案 0 :(得分:0)

根本不清楚你在这里想要确定哪一行是"重复"但是这个例子应该向你展示一种处理这种事情的非常直接的方式。

declare @Something table
(
    col1 int
    , col2 varchar(10)
    , col3 char(1)
    , col4 char(1)
    , col5 char(2)
)

insert @Something
values
(1, 'Goerge', 'A', 'B', '++')
, (2, 'Alex', 'B', 'B', 'aa')
, (2, 'Alex', 'B', 'B', '++')
;

with SortedValues as
(
    select *
        , ROW_NUMBER() over (partition by col1 order by col5 desc) as RowNum
    from @Something
)

delete SortedValues
where RowNum > 1

select *
from @Something

答案 1 :(得分:0)

使用CTE和ROW_NUMBER()查找(并删除)重复项

WITH CTE_Dup AS 
(
   SELECT *
   , ROW_NUMBER OVER() PARTITION BY (Col1, Col2, Col3, Col4 ORDER BY Col5) RN
   -- change ORDER BY criteria to make sure row you want to keep is 1st
 FROM YourTable
)
DELETE --Try SELECT * to check before actually deleting
FROM CTE_Dup WHERE RN>1

答案 2 :(得分:0)

您可以尝试以下查询。我对列进行了分组并得到了它的计数并处理了WHERE子句中的条件。

使用给定的样本数据执行查询:

DECLARE @TestTable TABLE (col1 INT, col2 VARCHAR (20), col3  VARCHAR (20), col4 VARCHAR (20), col5 VARCHAR (20));

INSERT INTO @TestTable (col1, col2, col3, col4, col5) VALUES
(1, 'Goerge', 'A', 'B', '++'),
(2, 'Alex', 'B', 'B', 'aa'),
(2, 'Alex', 'B', 'B', '++');

SELECT * INTO #tmpResults FROM (
    SELECT col1, col2, col3, col4, col5, 
        COUNT(*) OVER (PARTITION BY  col1, col2, col3, col4 ORDER BY col1 ) AS CCnt
    FROM @TestTable
) a

SELECT col1, col2, col3, col4, col5 FROM #tmpResults WHERE Ccnt = 1 AND Col5 = '++'
UNION
SELECT col1, col2, col3, col4, col5 FROM #tmpResults WHERE Ccnt > 1 AND Col5 <> '++';

DROP TABLE #tmpResults

<强>输出:

col1    col2    col3    col4    col5
------------------------------------
1       Goerge  A       B       ++
2       Alex    B       B       aa