SELECT [RelType].[Label], [RelType].[Type], [InvRelType].[Label] as [InvLabel], [InvRelType].[Type] From ([RelType] left Join [RelType] as InvRelType on ([RelType].[Id] = [InvRelType].[InvRelTypeId] and [InvRelType].[DBId] = @DBIdInvRelType@)) where (([InvRelType].[Label] = '' or [InvRelType].[Label] is null) Or ([RelType].[Type] = 0 And [InvRelType].[Type] = 0)) and [RelType].[DBId] = @DBIdRelType@ Order by [RelType].[Label]

SELECT [RelType].* From ([RelType] left Join [RelType] as InvRelType on ([RelType].[Id] = [InvRelType].[InvRelTypeId] and [RelType].[DBId] = [InvRelType].[DBId])) where (([InvRelType].[Label] = '' or [InvRelType].[Label] is null) Or ([RelType].[Type] = 0 And [InvRelType].[Type] = 0)) and [RelType].[DBId] in (Select [DBId] from [CompanyPerson] where [RelationId] = 0 Group by [DBId] having count(*) > 0) order by [RelType].[DBId], [RelType].[Id]