I will try to describe the real situation. In our company we have a reservation system with a table, letβs call it Customers, where the email and phone contacts are saved with each incoming order - which part of the system I canβt change. I ran into a problem how to get the number of unique customers. With a unique client, I mean a group of people who have the same email or the same phone number.
Example 1: From real life, you can imagine that Tom and Sandra are married. The volume that ordered 4 products filled our reservation system with 3 different email addresses and 2 different phone numbers when one of them shares with Sandra (like an intercom), so I can assume that they are somehow connected. Sandra, with the exception of this common telephone number, also filled out her personal, and for both orders she used only one email address. For me, this means taking all of the following lines as a unique client. Thus, in fact, this unique client can grow up in the whole family.
ID E-mail Phone Comment ---- ------------------- -------------- ------------------------------ 0 tom@email.com +44 111 111 First row 1 tommy@email.com +44 111 111 Same phone, different e-mail 2 thomas@email.com +44 111 111 Same phone, different e-mail 3 thomas@email.com +44 222 222 Same e-mail, different phone 4 sandra@email.com +44 222 222 Same phone, different e-mail 5 sandra@email.com +44 333 333 Same e-mail, different phone
As ypercube said I would probably need recursion to count all of these unique clients.
Example 2: Here is an example of what I want to do.
Is it possible to get the number of unique clients without using recursion, for example, using the cursor or something, or recursion is required?
ID E-mail Phone Comment ---- ------------------- -------------- ------------------------------ 0 linsey@email.com +44 111 111 ββ 1 louise@email.com +44 111 111 ββ 1. unique customer 2 louise@email.com +44 222 222 ββ ---- ------------------- -------------- ------------------------------ 3 steven@email.com +44 333 333 ββ 4 steven@email.com +44 444 444 ββ 2. unique customer 5 sandra@email.com +44 444 444 ββ ---- ------------------- -------------- ------------------------------ 6 george@email.com +44 555 555 βββ 3. unique customer ---- ------------------- -------------- ------------------------------ 7 xavier@email.com +44 666 666 ββ 8 xavier@email.com +44 777 777 ββ 4. unique customer 9 xavier@email.com +44 888 888 ββ ---- ------------------- -------------- ------------------------------ 10 robert@email.com +44 999 999 ββ 11 miriam@email.com +44 999 999 ββ 5. unique customer 12 sherry@email.com +44 999 999 ββ ---- ------------------- -------------- ------------------------------ ---------------------------------------------------------------------- Result β = 5 unique customers ----------------------------------------------------------------------
I tried a query with GROUP BY, but I don't know how to group the result by the first or second column. I'm looking to say something like
SELECT COUNT(*) FROM Customers GROUP BY Email OR Phone
Thanks again for any suggestions.
PS I really appreciate the answers to this question before the complete rephrasing. Now the answers here may not correspond to the update, so please do not click here if you intend to do this (except for the question, of course :) . I completely rewrote this post.
Thank you and sorry for the wrong start.