0

我在 SQL Server 中使用存储过程,但它给了我一些重复的记录,当然我的数据库中没有重复的记录,但是我的存储过程给了我相同记录的两个实例,可以是什么错误的?如何防止我的查询给出重复记录?这是我的 SP 选择子句:

select  (ROW_NUMBER() OVER (ORDER BY Review.Point desc) ) as rownumber, 

Business.BusinessId,Business.BName,Business.BAddress1
  ,Business.BAddress2,Business.BCity,Business.BState,Business.BZipCode,Business.countryCode,Business.BPhone1,Business.BPhone2,Business.BEmail,Business.Keyword
  ,Business.BWebAddress,Business.BCatId,Business.BSubCatId,Business.BDetail,Business.bImage,Business.UCId,Business.UCConfirm
  ,Business.UOId,Business.UOConfirm,Business.x,Business.y,Cat.CatName,SubCat1.SubCatName
 from Business left outer join 
        Review on business.BusinessId=Review.BusinessId left outer join
        Cat  on business.BCatid=Cat.CatId  left outer join 
        SubCat1  on business.BSubCatid=SubCat1.SubCatId '+@sql2+' 
) as tbl
where rownumber between  '+CONVERT(varchar, @lbound)+' and '+CONVERT(varchar, @ubound);
4

3 回答 3

1

我不知道要挖掘到您的联接逻辑的数据,但如果它跨 BusinessID 重复,您可以ROW_NUMBER()为重复项添加另一个:

select  (ROW_NUMBER() OVER (ORDER BY Review.Point desc) ) as rownumber, 
r = ROW_NUMBER()OVER(PARTITION BY Business.BusinessId ORDER BY Business.BusinessId)
Business.BusinessId,Business.BName,Business.BAddress1
  ,Business.BAddress2,Business.BCity,Business.BState,Business.BZipCode,Business.countryCode,Business.BPhone1,Business.BPhone2,Business.BEmail,Business.Keyword
  ,Business.BWebAddress,Business.BCatId,Business.BSubCatId,Business.BDetail,Business.bImage,Business.UCId,Business.UCConfirm
  ,Business.UOId,Business.UOConfirm,Business.x,Business.y,Cat.CatName,SubCat1.SubCatName
 from Business left outer join 
        Review on business.BusinessId=Review.BusinessId left outer join
        Cat  on business.BCatid=Cat.CatId  left outer join 
        SubCat1  on business.BSubCatid=SubCat1.SubCatId '+@sql2+' 
) as tbl
where rownumber between  '+CONVERT(varchar, @lbound)+' and '+CONVERT(varchar, @ubound)
AND r = 1;
于 2012-07-14T13:40:53.877 回答
0

在查询中包含保留字 DISTINCT。

例如

select distinct 
    *
from
    students s
    inner join enrollments e on e.StudentId = s.Id
    inner join courses c on c.Id = e.CourseId

但是,结果表中的意外重复通常(但不总是)表明您的查询格式错误或数据库设计错误。

于 2012-07-14T13:33:36.363 回答
0

尝试删除此左连接

Review on business.BusinessId=Review.BusinessId left outer join 

您的查询中似乎不需要,如果一项业务有多个评论...

于 2012-07-14T13:34:15.783 回答