sql - select rows with non distinct values in column 1 scoped to column 2 -
so have people table , bank_accounts table:
people id | name 1 john 2 mark 3 mary bankaccount id | person_id | currency 1 1 'usd' 2 1 'eur' 3 2 'usd' 4 2 'usd' 5 3 'eur' i want accounts owners if owner has accounts max 1 kind of currency. don't want any account owned user has account in currency. erm :p
so table want looks that:
account_id | person_id | currency 3 2(mark) 'usd' 4 2(mark) 'usd' 5 3(mary) 'eur' hope it's understandable. simplified example of course. use on lot bigger tables lot of data. efficiency good.
thanks lot time , help!
select * bankaccount person_id in ( select person_id bankaccount group person_id having count(distinct currency) = 1 )
Comments
Post a Comment