0

enter image description here This is my table in which the information about the owner of the apartment, the number of the house in which the apartment is located, etc. The owners of the same apartment can be two different people, for this is responsible column Fraction, which shows a certain part of the apartment owned by a person.

My task is to display all the information if one apartment is owned by two people?

Accordingly, as a result, I should get apartment number 9 in house number 14

2 Answers2

1

I think you want:

SELECT House_Number, Apartment_Number
FROM yourTable
GROUP BY House_Number, Apartment_Number
HAVING MIN(Owner) <> MAX(Owner);

The above logic will detect anyone house and apartment having more than one unique owner.

Abra
  • 19,142
  • 7
  • 29
  • 41
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Yes it works, but I don't understand the logic of this design: MIN(Owner) <> MAX(Owner) –  Apr 06 '22 at 07:19
  • @Araders If a given address has the _same_ single owner, then the min and max values will be the same as well, otherwise they will be different. – Tim Biegeleisen Apr 06 '22 at 07:21
1

This works for me:

select Owner
  from HOMES
 where (House_Number, Apartment_Number) in (select House_Number
                                                  ,Apartment_Number
                                              from HOMES
                                             group by House_Number
                                                     ,Apartment_Number
                                            having count(*) > 1)

See this db<>fiddle and also refer to this SO question:
MySQL multiple columns in IN clause

Abra
  • 19,142
  • 7
  • 29
  • 41