Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Duplicate rows occures in my query

Author  Topic 

hdv212
Posting Yak Master

140 Posts

Posted - 2010-11-04 : 10:40:16
Hi
i'm facing a problem to get a query. i have a query which have a simple join with another table.
plz download and exevute my sample db script to reproduce the problem (plz skip some errors during creating database):
http://cid-1490cc59dd735cbf.office.live.com/self.aspx/.Public/ProblemTest.sql

1. plz execute first query :

select * from dbo.Reception

Comment : As u can see in the result, we have a single record in result Set.

2. then, plz execute second query :

select * from dbo.InsuranceTarrifs

Comment : This table have relation with Reception table via Reception.BasicInsuranceID and InsuranceTarrifs.InsuranceID fields.

3. nowm we want to query from both table and join together as follow :

select r.* from Reception r
inner join InsuranceTarrifs it on r.BasicInsuranceID=it.InsuranceID

As u think like me, we expect get a single row in result set, but there is a 2 rows in result set which the second row as the same as the first one (duplicate row).

where is my problem and how to solve this ?

thanks

Sachin.Nand

2937 Posts

Posted - 2010-11-04 : 13:44:06
Why do you think it should give just one record?
Inner join loops through each row in the outer table and returns only the rows for which there is an equal value in the join column.
So there are 2 records matching in your outer table InsuranceTarrifs which matches the join column value of BasicInsuranceID on the inner table Reception

quote:
where is my problem and how to solve this ?


You have to decide which row value would you need in case the query returns multiple rows.

PBUH

Go to Top of Page
   

- Advertisement -