views:

164

answers:

6

I want to Select tblProperty.ID only when this query returns greater than 0

SELECT     
    COUNT(tblProperty.ID) AS count
FROM         
    tblTenant AS tblTenant 
    INNER JOIN tblRentalUnit
        ON tblTenant.UnitID = tblRentalUnit.ID 
    INNER JOIN tblProperty
        ON tblTenant.PropertyID = tblProperty.ID 
        AND tblRentalUnit.PropertyID = tblProperty.ID
WHERE tblProperty.ID = x

Where x is equal to the parent's tblProperty.ID that it is looking at. I do not know what 'x' is.

How can I do this?

Database Structure:
tblTenant:
  ID
  PropertyID <--foreign key to tblProperty
  UnitID     <--foreign key to tblRentalUnit
  Other Data
tblProperty:
  ID
  Other Data
tblRentalUnit:
  ID
  PropertyID <--foreign key to tblProperty
  Other Data

Explanation of the query:
The query is select only the properties that have rental units that have tenants living in them.

+3  A: 
SELECT     
   tblProperty.ID
FROM         
    tblTenant AS tblTenant 
    INNER JOIN tblRentalUnit AS tblRentalUnit 
        ON tblTenant.UnitID = tblRentalUnit.ID 
    INNER JOIN tblProperty AS tblProperty 
        ON tblTenant.PropertyID = tblProperty.ID 
        AND tblRentalUnit.PropertyID = tblProperty.ID
GROUP BY tblProperty.ID
HAVING COUNT(tblProperty.ID) > 1

Should work.

pgb
What do I replace 'x' with?
Malfist
If I understand it correctly, you need a group by then. You want only the groups (groupd by tblProperty.id) that have a count of more than 1.
pgb
Actually I found a much simpler solution. Posting it in next comment
Malfist
SELECT p.IDFROM tblProperty AS p LEFT OUTER JOIN tblTenant AS t ON t.PropertyID = p.IDWHERE (t.UnitID IS NOT NULL)
Malfist
yours doesn't return the right results...
Malfist
I think if you change the HAVEING line to > 0 you would get the proper results.
Malfist
whoops, my bad, it works according to how I described the problem.
Malfist
This is wrong? HAVING COUNT() > 1 will exclude a property with 1 rentalunit and 1 tennant.
Andomar
@andomar, read my comment after that one.
Malfist
A: 

GROUP BY clause, perhaps? SELECT INTO a temp table & then SELECT from #tmp, if that's easier.

Garrett
+1  A: 

Add the following to the end of the query. This assumes that you do not want anything to return if the count is 1 or 0.

HAVING COUNT(tblProperty.ID) > 1
dpmattingly
A: 

what about changing the start to SELECT tblProperty.ID and add at the end HAVING COUNT(tblProperty.ID) > 1? Though I admit I don't understand your AS clauses -- they seem totally redundant to me, each and every one...

Alex Martelli
except the Count one, crystal reports did that for me and I never took them out.
Malfist
+2  A: 

The query is: select only the properties that have rental units that have tenants living in them.

SELECT
  p.ID
FROM
  tblProperty              AS p
  INNER JOIN tblRentalUnit AS u ON u.PropertyID = p.ID
  INNER JOIN tblTenant     AS t ON t.UnitID     = u.ID
GROUP BY
  p.ID

This should do it. The inner joins to explicitly not select any unreferenced records, that means this selects only those properties that have rental units that have tenants.

I'm not sure why your tblTenant links to the tblProperty. It looks as though this was not necessary, since the link seems to go from tenant->rental unit->property.

Tomalak
This also works.
Malfist
A: 

Actually, this works:

SELECT DISTINCT
     p.ID
FROM         tblProperty AS p LEFT OUTER JOIN
                      tblTenant AS t ON t.PropertyID = p.ID
WHERE     (t.UnitID IS NOT NULL)
Malfist
Could you check if my query also works? And give an answer to the question I have about your data layout? Thanks :)
Tomalak
Didn't give answer because you weren't the first. But I did give you a deserved upvote :)
Malfist
You understand that this is bad solution, right? If you use a left join you then completely negate it by putting a refernce to that table inthe where clause unless you are asking for instances where the id is null (which gives you records in the first table and not the second). So an inner join would be better for performance as well as simpler. Also do you really have tenants living in more than one property? If not you don't need to waste time with a distinct either.
HLGEM
@Malfist: I'm at my 200 rep limit for today already, the up-vote did not count anymore. ;-) But why does the tenant table reference the property table? This seems like a normalization error to me...
Tomalak
It is, and I'm talking to my boss about that right now. It makes no sense to me. There's a few other things like this too. The project was outsourced overseas and brought back when they couldn't do it, so I got stuck with it. There are more than a few...odd things about it. And no comments.
Malfist