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 2000 Forums
 Transact-SQL (2000)
 joins

Author  Topic 

veeramaga
Starting Member

5 Posts

Posted - 2006-10-22 : 00:46:13
select distinct p.property_id,count(r.property_id)as total_ region,count(t.property_id) as total_tenant
from Property p
inner join
region r
on p.property_id =r.property_id
inner join
tenant t
on r.property_id = t.property_id
and p.property_id= 12
group by p.property_id

The above query is to get me the count of region and tenants for each property

in the above query I added 5 rows of property_id as 12,when I execute it separately that is, property table and region table I get 5 as the count which is correct, the same way with property table and tenant table the same 5 as count, but if I run like above, joining 3 tables I get the output of count as 25, why is it?can anyone help me out in finding out the reason?

nr
SQLTeam MVY

12543 Posts

Posted - 2006-10-22 : 09:08:47
try
count(distinct r.region_id)as total_ region,count(distinct t.tenant_id) as total_tenant

if you have those columns.
Get rid of the group by and do a select * to see why you are getting the results.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -