获取重复值的sql查询

获取重复值的sql查询,sql,sql-server,tsql,Sql,Sql Server,Tsql,嗨,我正在使用下面的查询 Create proc [dbo].[usp_getPropertyByWord] (@Description nvarchar(200)) as set @Description=replace(@Description,' ',' ') set @Description=replace(@Description,' ',' ') create table #temp(name varchar(100)) insert into #tem

嗨,我正在使用下面的查询

Create proc [dbo].[usp_getPropertyByWord]  
(@Description nvarchar(200))  
as  

set @Description=replace(@Description,'   ',' ')  
set @Description=replace(@Description,'  ',' ') 

create table #temp(name varchar(100)) 
insert into #temp valueS(@Description)
insert into #temp 
SELECT word FROM dbo.splitStringChars(@Description,' ',',')
delete #temp where name in('',' ',',')

create table #temp1(property_id int,property_number nvarchar(15),name nvarchar(500),short_desciption nvarchar(2000),description nvarchar(4000),address1 nvarchar(200),address2 nvarchar(200),city nvarchar(50),locality nvarchar(100),zip nvarchar(8)) 
insert into #temp1(property_id,property_number,name ,short_desciption,description,address1,address2,city,locality,zip)
SELECT distinct(p.property_id),p.property_number,p.name,p.short_description,p.description,ta.address1,ta.address2,
tc.city_name,cl.City_Locality_Name,ta.zip
FROM tbl_property p  
INNER JOIN 
tbl_PropertyAddress ta on ta.property_id=p.property_id
inner join tbl_City tc on ta.city=tc.city_id
inner join  tbl_City_Locality cl on cl.City_id=tc.city_id
inner join
(SELECT distinct(name) FROM #temp) B  
ON (p.name=@Description or p.name LIKE '%' + B.name + '%' or p.short_description like '%' + B.name + '%' or p.description like '%' + B.name + '%'
or ta.address1 LIKE '%' + B.name + '%' or ta.address2 LIKE '%' + B.name + '%' or tc.City_Name LIKE '%' + B.name + '%')  
where  NOT EXISTS(select t.* from #temp1 t where t.property_id=p.property_id and t.property_number=p.property_number and t.name=p.name and t.short_desciption=p.short_description and t.description=p.description and t.address1=ta.address1 and t.address2=ta.address2 and t.city=tc.City_Name and t.locality=cl.City_Locality_Name and t.zip=ta.zip)


  delete  #temp
  select  *  from #temp1
GO
如果我给@Description加上“hyderbad hyd,hgf,hyds”,我会得到16000条记录,但我的表中只有130条记录

我从splitStringChars函数中获取值

hyderbad     
hyd    
hgf    
hyds  
如何解决这个问题?

你知道TSQL吗

Create proc [dbo].[usp_getPropertyByWord]  
(@Description nvarchar(200))  
as  

set @Description=replace(@Description,'   ',' ')  
set @Description=replace(@Description,'  ',' ') 

create table #temp(name varchar(100)) 
insert into #temp valueS(@Description)
insert into #temp 
SELECT word FROM dbo.splitStringChars(@Description,' ',',')
delete #temp where name in('',' ',',')


SELECT p.property_id,p.property_number,p.name,p.short_description,p.description
-- ,ta.address1,ta.address2,tc.city_name,cl.City_Locality_Name,ta.zip
FROM tbl_property p  
-- INNER JOIN 
--tbl_PropertyAddress ta on ta.property_id=p.property_id
--inner join tbl_City tc on ta.city=tc.city_id
--inner join  tbl_City_Locality cl on cl.City_id=tc.city_id
inner join
(SELECT distinct(name) FROM #temp) B  
ON (p.name=@Description or p.name LIKE '%' + B.name + '%' or p.short_description like '%' + B.name + '%' or p.description like '%' + B.name + '%'
-- or ta.address1 LIKE '%' + B.name + '%' or ta.address2 LIKE '%' + B.name + '%' 
-- or tc.City_Name LIKE '%' + B.name + '%'
)  
where  NOT EXISTS(select t.* from #temp1 t where t.property_id=p.property_id and t.property_number=p.property_number and t.name=p.name and t.short_desciption=p.short_description and t.description=p.description 
-- and t.address1=ta.address1 and t.address2=ta.address2 and t.city=tc.City_Name and t.locality=cl.City_Locality_Name and t.zip=ta.zip
)

听起来一个不好的连接导致了多对多关系。。。您是否尝试过对一些复杂性进行注释以确定是哪个联接导致了问题?如果联接中的一个列值不同,即使使用distinct也可能不会仅返回一行。遵循Kevin的建议,消除复杂性,直到你发现什么时候不再得到倍数。这将帮助您找出导致多条记录的表和列。然后告诉我如何使用join我需要签入所有这些列。我的问题很清楚,但它们是否为负。如果join是多对多的,请告诉我如何减少…这是基本的调试技术:从最内部的查询开始,确保它返回正确的数据。然后,一个接一个地,通过JOIN添加额外的表,看看哪里突然出现了重复的表,然后你需要用你的大脑来理解为什么会出现重复的表并修复它。我们不能真正为您做这项工作-我们手头没有您的数据库来进行测试。。。