Reusing select statement I am checking if there are any rows in a table with some conditions.  If there are more than 1 row, it should filter again with some more conditions.

DECLARE @Matches AS int
DECLARE @Result AS int

SET @Matches =  
(
    Select COUNT(*)
        FROM Houses 
            INNER JOIN Houses_Access ON Houses.ID = Houses_Access.DoorKey
            FULL OUTER Join UserAccess ON  Houses_Access.GroupKey  = UserAccess.GroupKey
        WHERE Houses.ID IS NOT NULL AND Houses.ID = @DoorKey AND UserAccess.UserKey = @UserKey
)

IF(@Matches &lt; 1)
 SET @Result = 0
ELSE IF(@Matches &gt; 0)
    BEGIN
        SET @Matches =  
        (
        Select COUNT(*)
        FROM Houses 
            INNER JOIN Houses_Access ON Houses.ID = Houses_Access.DoorKey
            FULL OUTER Join UserAccess ON  Houses_Access.GroupKey  = UserAccess.GroupKey
        WHERE Houses.ID IS NOT NULL AND Houses.ID = @DoorKey AND UserAccess.UserKey = @UserKey
            AND Houses_Access.GroupKey != Houses.OwnerGroupKey 
        )
        IF(@Matches &lt; 1)
            SET @Result = 0
        ELSE
            SET @Result = 1
    END

RETURN(@Result)

END
Create a view vw_HousesAccess  which will contain following query and then



        Create View vw_HousesAccess  
        as 
        Select * --TODO specify columns where 
        FROM Houses 
            INNER JOIN Houses_Access ON Houses.ID = Houses_Access.DoorKey
            FULL OUTER Join UserAccess ON  Houses_Access.GroupKey  = UserAccess.GroupKey

        ---then in main query you can use like this. 
        IF Exists(Select 1 from vw_HousesAccess  where  WHERE ID IS NOT NULL AND ID = @DoorKey AND UserKey = @UserKey)
         ---your logic