2

我想删除重复项,但它们不会显示为重复项。有没有一个查询可以帮助删除这个只留下一个?

在此处输入图像描述

4

2 回答 2

2

此查询将产生标准化值(如果它们都是 varchars),您现在可以检查重复项

select 
    [someId], 
    case 
       when reg_num < driver_name then reg_num + driver_name 
                                  else driver_name + reg_num 
    end as combination
于 2013-11-05T07:21:49.017 回答
0

像这样的东西将按两个字段按排序顺序对行进行分组,并删除除一个之外的所有行。我假设这两个字段必须匹配,并且您有一个名为 TableId 的主键。

DELETE
    T
FROM
    [Table] T
JOIN (
    SELECT
        TableId,
        [Name],
        ROW_NUMBER() OVER ( PARTITION BY [Name] ORDER BY TableId DESC ) RowId
    FROM (
        SELECT
            TableId,
            Reg_Num + '|' + Driver_Name AS [Name]
        FROM
            [Table]
        WHERE
            Reg_Num <= Driver_Name
        UNION
        SELECT
            TableId,
            Driver_Name + '|' + Reg_Num AS [Name]
        FROM
            [Table]
        WHERE
            Driver_Name < Reg_Num
        ) Y
    ) X ON
        X.TableId = T.TableID
WHERE
    RowId > 1 
于 2013-11-05T07:39:54.120 回答