2012-05-17 90 views
-2

嗨我正在使用下面的查詢。SQL查詢獲取重複值

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 ([email protected] 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 

如果我給「 'hyderbad路政署,HGF,hyds'」爲@Description我得到16000條記錄,但我只有130在我的表中的記錄。

我正在從splitStringChars函數值

hyderbad  
hyd  
hgf  
hyds 

這又如何解決呢?

+2

聽起來像一個不好的JOIN導致多對多的關係......你有沒有試過評論一些複雜性來確定哪個連接導致你的問題? –

+1

使用'distinct' ... – Rahul

+0

即使使用distinct,如果聯接中的某個列值不同,則可能不會只返回單個行。遵循凱文的建議,並消除複雜性,直到找到何時停止獲得倍數。這應該可以幫助您找出哪個表和列導致多條記錄。 – Josh

回答

0

你知道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 ([email protected] 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 
)