I need to group users so that they are grouped together if they have the same name or email. That is, to combine them into one group it is not necessary that everyone has the same e-mail or name, it is enough that the name or e-mail coincides with at least one other user. The desired result in the picture.

I've tried using Dense_Rank for name and email, but I don't know if I can combine them.

I would also be grateful if you tell me where I can find information on this topic, since at the moment I don’t even know how to correctly formulate a query in Google

create table #a (UserID int, Name varchar(25), Email Varchar(50))

insert into #a

values

(1, 'Marvin Cottrell', '123456@gmail.com'),

(2, 'Colleen Morrison', 'somebody@gmail.com'),

(3, 'Colleen Morrison', 'cm@gmail.com'),

(4, 'Dante Henry', '765@gmail.com'),

(5, 'Willie Alcock', '123456@gmail.com'),

(6, 'Marvin Cottrell', 'lib@gmail.com'),

(7, 'Cecilia Mueller', '765@gmail.com'),

(8, 'Cathal Compton', '765@gmail.com'),

(9, 'Giovanni Tapia', '123456@gmail.com'),

(10, 'Humphrey Macias', 'somebody@gmail.com')select *, DENSE_RANK () over (order by Name desc) as DENSE

from #aselect *, DENSE_RANK () over (order by Email desc) as DENSE

from #a