Home > Mobile >  Get rows with the same value
Get rows with the same value

Time:04-06

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 owns two people?

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related