SQL删除某列中的重复项

时间:2017-07-19 13:54:52

标签: sql sql-server database

大家下午好!

有关于删除一列中重复项并将其删除整行的问题。我将在Excel的屏幕截图中提供一个示例,以便不提供专有信息。

我希望删除其中一个以黄色突出显示的行,但不希望将其限制为一个Dr.Mike或一个Health Partners诊所。相对较新,所以任何帮助都将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:1)

你可以这样做:

select distinct prov, clinic, address
from t;

答案 1 :(得分:-1)

我已使用以下脚本删除重复项。

SELECT Prov, Clinic,Address, count(*)
  FROM SomeTable
  group by Prov, Clinic,Address
  having count(*)>1



  SELECT Prov, Clinic,Address, countrows = count(*)
INTO [holdkey]
FROM SomeTable
GROUP BY Prov, Clinic,Address
HAVING count(*) > 1



  SELECT DISTINCT sometable.*
INTO holdingtable
FROM sometable, holdkey
WHERE sometable.Prov = holdkey.Prov
AND sometable.Clinic = holdkey.Clinic
AND sometable.Address = holdkey.Address

SELECT Prov, Clinic,Address, count(*)
  FROM holdingtable
  group by Prov, Clinic,Address
  having count(*)>1

DELETE sometable
FROM sometable, holdkey
WHERE sometable.Prov = holdkey.Prov
AND sometable.Clinic = holdkey.Clinic
AND sometable.Address = holdkey.Address

INSERT sometable SELECT * FROM holdingtable