如何删除重复的行

时间:2015-04-23 00:51:32

标签: sql sql-server

我有以下查询来检索一堆数据但不知何故它为某些记录创建了重复的行。我尝试了不同但没有用的

我在这里做错了什么

  SELECT Distinct dbo.tblAssessmentEcosystemCredit.ManagementZoneID, AssessmentEcosystemCreditID,dbo.tblAssessmentEcosystemCredit.AssessmentVersionID , 
(COALESCE(dbo.tblManagementZone.SiteValueCurrentScore,0) 
-COALESCE(dbo.tblManagementZone.SiteValueFutureScore,0)) AS LossinSiteValueScore, 
5 AS SaveType, dbo.ufn_varbintohexstr(dbo.tblAssessmentEcosystemCredit.RowTimestamp) AS RowTimestamp, 
dbo.tblVegetationZone.EECID, 
CASE WHEN dbo.tblVegetationZone.EECID > 0 THEN 3.0 
ELSE 1.0 
END AS EECOffSetMultiplier 
FROM dbo.tblAssessmentEcosystemCredit 
INNER JOIN dbo.tblVegetationType 
ON dbo.tblAssessmentEcosystemCredit.VegTypeID = dbo.tblVegetationType.VegTypeID 
INNER JOIN dbo.tblManagementZone 
ON dbo.tblAssessmentEcosystemCredit.ManagementZoneID = dbo.tblManagementZone.ManagementZoneID 
INNER JOIN dbo.tblVegetationZone 
ON dbo.tblVegetationZone.VegetationZoneID = dbo.tblManagementZone.VegetationZoneID 
INNER JOIN dbo.tblAssessmentVersion AV ON 
AV.AssessmentVersionID = dbo.tblAssessmentEcosystemCredit.AssessmentVersionID 
INNER JOIN tblAssessment TBA ON 
TBA.AssessmentID = AV.AssessmentID 
WHERE dbo.tblAssessmentEcosystemCredit.AssessmentVersionID= @AssessmentVersionID

1 个答案:

答案 0 :(得分:1)

您的中央基表dbo.tblAssessmentEcosystemCredit可能有重复的行。这应该很容易检查,因为您知道要查看哪些行。

更有可能的是,您正在获取与少数dbo.tblAssessmentEcosystemCredit行相对应的多个结果行,因为您要加入的其中一个表对这些行有多个匹配项。也就是说,其中一列包含至少一个重复值:

dbo.tblVegetationType.VegTypeID 
dbo.tblManagementZone.ManagementZoneID 
dbo.tblVegetationZone.VegetationZoneID
dbo.tblAssessmentVersion.AssessmentVersionID
tblAssessment.AssessmentID

负责列不得受单列UNIQUE约束,并且不得为其表的单列主键,因此可能有助于缩小范围。请注意,整行不需要重复,只需要ID。