user17336953
user17336953

Reputation:

Get rows with the same value

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

Upvotes: 0

Views: 34

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

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.

Upvotes: 1

Abra
Abra

Reputation: 20924

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

Upvotes: 1

Related Questions