I believe you want:
select user_id
from t
group by user_id
having count(*) = count(distinct firstname) and
count(*) = count(distinct lastname);
I'm not sure if you want the names as pair. If so:
having count(*) = count(distinct firstname, lastname)
Not all databases support multiple arguments to count(distinct)
. If this is what you really intend, it is easy enough to phrase without using this.
select user_id,count(*)
from table
group by user_id having
count(distinct firstname) =count(firstname) and count(distinct
lastname)=count(lastname) ;
This gives those userids where on grouping userids if distinct fname and lname are greater than 1 for each userid group that means that group contains no duplicates.
Following SQL will work - it will also work if you would add lets say this row to your sample data (8,7, 'will', 'Rj') making User_id 7 having 4 rows but two of them with identical names.
with temp as (
SELECT user_id, firstname, lastname
, count(*) over (partition by user_id, firstname, lastname) as counter
FROM t2
)
SELECT * --distinct user_id
FROM temp t
WHERE not exists (SELECT 1 FROM temp WHEE counter = 2 and user_id = t.user_id)
The statements returns all rows with names unless you change the * to the commented distinct user_id then it will only return the user_id having ONLY unique names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.