Question

I have two tables 'Customer' and 'CustomerCommuncation' which have fields :

 **Customer**
---------------------------------------------
|ID     |    FirstName    |    LastName      |
| 1     |    John         |    Menon         | 
| 2     |    george       |    cool          | 
| 3     |    John         |    Menon         | 
| 4     |    george       |    cool          |
| 5     |    John         |    Menon         |  



 **CustomerCommunication**
---------------------------------------------------------
|ID     |    CommValue              |    CustomerID     |
| 1     |    abc@gmail.com          |    1              | 
| 2     |    abcd@gmail.com         |    1              | 
| 3     |    9000000000             |    1              | 
| 4     |    abcde@gmail.com        |    1              | 
| 5     |    xyz@gmail.com          |    2              | 
| 6     |    xyzw@gmail.com         |    2              | 
| 8     |    9000000000             |    3              | 
| 9     |    abcdef@gmail.com       |    3              | 
| 10    |    9000000000             |    5              | 
| 11    |    xyz@gmail.com          |    4              | 

These two table can be collectively described as : John menon has can be contacted on: abc@gmail.com,abcd@gmail.com,abcde@gmail.com 9000000000.

Two customer are similar when their Firstname,LastName and atleast one of the CommValue is similar.Clearly customerIDs 1,3 and 5 are similar since they have same FirstName,LastName and shares a PhoneNumber 9000000000.

Question : Given a list of customerID into a Group. we have to find union of CustomerID which are similar to Customers present in the Group.

For example if we are given a Group(1,2) than result would be (3,4,5)

I have written stored procedure like:

CREATE DEFINER= FUNCTION `stroredfunc`( custlist varchar(1000)) RETURNS varchar(10000) 
{
begin
declare v_commvalue varchar(10000) default '';
declare v_LastName varchar(1000) default '';
declare v_firstname varchar(1000) default '';
declare v_result varchar(10000) default '';

select group_concat(Distinct c.FirstName SEPARATOR ',') as v_firstname,
group_concat(Distinct c.LastName SEPARATOR ',') as v_LastName,
group_concat(Distinct cc.CommValue SEPARATOR ',') as v_commvalue 
from Customer as c inner join CustomerCommunication as cc 
on cc.CustomerID=c.ID where FIND_INSET(c.ID,custlist);


select group_concat(distinct cc.CustomerID) into v_result  from Customer as c inner join
CustomerCommunication as cc
on cc.CustomerID=c.ID where  FIND_IN_SET(c.FirstName,v_firstname) and
FIND_IN_SET(c.LastName,v_LastName) and
FIND_IN_SET(cc.CommunicationValue,v_commvalue )
;   

return v_result;
end

}

Is there any other optimized way to do it

Était-ce utile?

La solution

Possibly can be reduced to a single piece of SQL.

I have assumed that you want a list of the ids of similar to each named id (ie, that 1 is similar to 3 and 5 and that 2 is similar to 4):-

SELECT a.ID, a.FirstName, a.LastName, GROUP_CONCAT(DISTINCT b.Id)
FROM Customer a
INNER JOIN Customer b
ON a.FirstName = b.FirstName
AND a.LastName = b.LastName
AND a.Id != b.Id
INNER JOIN CustomerCommunication c
ON a.Id = c.CustomerID 
INNER JOIN CustomerCommunication d
ON a.Id = d.CustomerID 
AND c.CommValue = d.CommValue
WHERE a.Id IN (1,2)
GROUP BY a.ID, a.FirstName, a.LastName

Example knocked up on SQL fiddle:-

http://www.sqlfiddle.com/#!2/cf054/1

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top