私はあなたが言うことを次のように解釈するつもりです:「そしてユーザー名は支配的な名前とは異なります」。
したがって、問題は最も一般的な名前を見つけてから、それ以外のすべての行を見つけることです。
with tgrp as (select tu.userid, tu.username, count(*) as cnt
from tblUser tu
group by tu.userid, tu.username
),
tmax as (select tu.userid, tu.username, cnt
from tgrp join
(select tu.userid, max(cnt) as maxcnt
from tgrp
group by tu.userid
) t1
on tgrp.userid = t1.userid and tgrp.cnt = t1.maxcnt
)
select *
from tblUser tu left outer join
tmax
on tu.userid = tmax.userid and tu.username = tmax.username
where tmax.userid is null
このクエリは段階的に実行します。idとnameのすべての組み合わせのカウントを検索します。次に、最大の組み合わせを見つけます。次に、最大の組み合わせではないものをすべて見つけます。
複数の名前を持つユーザーIDが必要な場合は、次を使用できます。
select userid
from tblUser tu
group by userid
having count(distinct username) > 1
完全なレコードを取得するには、これを元のテーブルに結合します。
select *
from tbl_user tu
where tu.userid in (select userid
from tblUser tu
group by userid
having count(distinct username) > 1
)