答案 0 :(得分:2)
我相信你想要
select user_id
from t
group by user_id
having count(*) = count(distinct firstname) and
count(*) = count(distinct lastname);
我不确定您是否要将名称配对。如果是这样:
having count(*) = count(distinct firstname, lastname)
并非所有数据库都支持count(distinct)
的多个参数。如果这是您的真正意图,那么无需使用它就可以很容易地表达出来。
答案 1 :(得分:0)
select user_id,count(*)
from table
group by user_id having
count(distinct firstname) =count(firstname) and count(distinct
lastname)=count(lastname) ;
如果每个用户ID组的不同的fname和lname大于1,这意味着该组不包含重复项,这将为这些用户ID分组用户ID。
答案 2 :(得分:0)
以下SQL将起作用-如果您要在示例数据中添加此行(例如8,7,'will','Rj'),则该行也将起作用,从而使User_id 7具有4行,但其中两行具有相同的名称
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)
该语句返回所有具有名称的行,除非您将*更改为带注释的不同的user_id,否则它将仅返回仅具有唯一名称的user_id。