[英]How to compare column values/rows in same table
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)
. 并非所有数据库都支持
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.
如果每个用户标识组的不同的fname和lname大于1,则表示这些用户标识在分组用户标识的位置上,这意味着该组不包含重复项。
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. 遵循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)
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. 该语句返回所有具有名称的行,除非您将*更改为带注释的不同的user_id,否则它将仅返回仅具有唯一名称的user_id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.