I'll try to describe the real situation. In our company we have a reservation system with a table, let's call it Customers, where e-mail and phone contacts are saved with each incoming order - that's the part of a system I can't change. I'm facing the problem how to get count of unique customers. With the unique customer I mean group of people who has either the same e-mail or same phone number.
Example 1: From the real life you can imagine Tom and Sandra who are married. Tom, who ordered 4 products, filled in our reservation system 3 different e-mail addresses and 2 different phone numbers when one of them shares with Sandra (as a homephone) so I can presume they are connected somehow. Sandra except this shared phone number filled also her private one and for both orders she used only one e-mail address. For me this means to count all of the following rows as one unique customer. So in fact this unique customer may grow up into the whole family.
ID E-mail Phone Comment
---- ------------------- -------------- ------------------------------
0 [email protected] +44 111 111 First row
1 [email protected] +44 111 111 Same phone, different e-mail
2 [email protected] +44 111 111 Same phone, different e-mail
3 [email protected] +44 222 222 Same e-mail, different phone
4 [email protected] +44 222 222 Same phone, different e-mail
5 [email protected] +44 333 333 Same e-mail, different phone
As ypercube said I will probably need a recursion to count all of these unique customers.
Example 2: Here is the example of what I want to do.
Is it possible to get count of unique customers without using recursion for instance by using cursor or something or is the recursion necessary ?
ID E-mail Phone Comment
---- ------------------- -------------- ------------------------------
0 [email protected] +44 111 111 ─┐
1 [email protected] +44 111 111 ├─ 1. unique customer
2 [email protected] +44 222 222 ─┘
---- ------------------- -------------- ------------------------------
3 [email protected] +44 333 333 ─┐
4 [email protected] +44 444 444 ├─ 2. unique customer
5 [email protected] +44 444 444 ─┘
---- ------------------- -------------- ------------------------------
6 [email protected] +44 555 555 ─── 3. unique customer
---- ------------------- -------------- ------------------------------
7 [email protected] +44 666 666 ─┐
8 [email protected] +44 777 777 ├─ 4. unique customer
9 [email protected] +44 888 888 ─┘
---- ------------------- -------------- ------------------------------
10 [email protected] +44 999 999 ─┐
11 [email protected] +44 999 999 ├─ 5. unique customer
12 [email protected] +44 999 999 ─┘
---- ------------------- -------------- ------------------------------
----------------------------------------------------------------------
Result ∑ = 5 unique customers
----------------------------------------------------------------------
I've tried a query with GROUP BY but I don't know how to group the result by either first or second column. I'm looking for let's say something like
SELECT COUNT(*) FROM Customers
GROUP BY Email OR Phone
Thanks again for any suggestions
P.S.
I really appreciate the answers for this question before the complete rephrase. Now the answers here may not correspond to the update so please don't downvote here if you're going to do it (except the question of course :). I completely rewrote this post.
Thanks and sorry for my wrong start.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…