Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在没有标识列时删除重复值_Sql - Fatal编程技术网

Sql 在没有标识列时删除重复值

Sql 在没有标识列时删除重复值,sql,Sql,我有一张临时桌子,看起来有点像下面: SheetID和PersonId是唯一的,因此通过查看上述数据,您可以看到它们是重复的。上述情况在我所查看的数据中非常常见,其中有重复的记录,FirstName和姓氏为NULL。这些行没有ID列 如何删除这些记录?如果要删除两列中任何一列缺少名称的重复记录,可以使用: delete from t where t.surname is null and (exists (select 1 fro

我有一张临时桌子,看起来有点像下面:

SheetID和PersonId是唯一的,因此通过查看上述数据,您可以看到它们是重复的。上述情况在我所查看的数据中非常常见,其中有重复的记录,FirstName和姓氏为NULL。这些行没有ID列


如何删除这些记录?

如果要删除两列中任何一列缺少名称的重复记录,可以使用:

delete from t
    where t.surname is null and
          (exists (select 1
                   from t t2
                   where t2.sheetId = t.sheetId and t2.surname is not null
                  ) or
           exists (select 1
                   from t t2
                   where t2.personid = t.personid and t2.surname is not null
                  )
          );
注意:我将exists分为两个子句,以便数据库可以使用索引(如果sheetid、姓氏和personid、姓氏可用)


我还猜测,只检查姓氏是否为空就足够了。

请用您正在使用的数据库标记您的问题:oracle、sql server等等?您想要什么结果?您是在寻找选择查询还是删除查询?您在尝试此操作时忘了包含代码。