Hi I'm currently trying to create a query to group customers together.
I'm currently wanting to group by "Phone Number", "Email" and maybe some other fields in the future.
My problem is that I may want to group rows which aren't directly linked but have mutual rows.
For example, in this diagram we can see that Customer 2 and Customer 5 are in no way related at all, however they both share connections with Customer 1 which would then allow them to group together.
Venn Diagram Describing my ideal Data Set
Here is another View: CustomerId | FullName | PhoneNumber | Email --|--|--|--------------------------------------------------------------------------- 1 | Bill Smith | 01612345678 | BillS@SqlTeam.com 2 | Kelly Smith | 01612345678 | KellyS@SqlTeam.com 3 | Kevin Roach | 07111111111 | KevinR@Example.com 4 | Chris Ronald | 07222222222 | ChrisR@Blog.co.uk 5 | Bill Smith | 07987654321 | BillS@SqlTeam.com
We can see that Customer 2 is connected to Customer 1 through a mutual Phone Number, and Customer 5 is connected to 1 through a mutual Email Address. However Customer 2 and Customer 5 share no information which would've originally grouped them.
My reasoning for needing this functionality is that one person / household could have multiple customer accounts all with differing information therefore I am trying to group them up as best as I can with as much mutual information as possible.
I have already tried to use DENSE_RANK()
and GROUP BY
, however these seem the additionally partition the groups when adding more than 1 column, I want a group to be created solely on the fact that a data field matches another.