我正在尝试为每个案例的多个组中的用户返回UserGroup表中的所有列。
表结构:
Case Group LastName FirstName
A 1 James Mason
B 2 John Abel
B 3 John Abel
D 4 Gordon Cathy
E 5 Baker Phil
F 6 Green Goldie
所需结果:
Case Group LastName FirstName
B 2 John Abel
B 3 John Abel
我能够运行此查询以返回属于多个组的案例中的重复用户列表,但不会列出他们所属的组。
SELECT case, lastname, firstname, count(*) FROM table
GROUP BY case, lastname, firstname
HAVING count(*) > 1
谢谢!
答案 0 :(得分:0)
我建议只使用exists
:
select t.*
from t
where exists (select 1
from t t2
where t2.case = t.case and
t2.firstname = t.firstname and
t2.lastname = t.lastname and
t2.group <> t.group
);