简体   繁体   中英

Join a table to itself with no specfic ID

Join a table to itself with no specfic ID

I have a table of customer records which has no ID column, a customer may appear in the table more than once with various of the columns populated. I have managed to build a column INIT_SURNAME which is fully poplated but not unique to a customer. How can I merge the rows for matching customers where they have the same INIT_SURNAME and share one other common data element. For example I want to return 3 cutomers from the following table, the 1st 3 rows are the same person.

eg

INPUT:

INIT_SURNAME|EMAIL|PHONE|OTHERID
J*SOAP|js@x.com|4321|
J*SOAP|js@x.com||
J*SOAP||4321|4
J*SOAP|otherjoe@y.com||2
J*DOE||12345|3

Output:

INIT_SURNAME|EMAIL|PHONE|OTHERID
J*SOAP|js@x.com|4321|4
J*SOAP|otherjoe@y.com||2
J*DOE||12345|3

Can this be done?

You could merge each column separately.

Example code in T-Sql:

declare @foo table ( 
    name varchar(30), 
    email varchar(30), 
    phone varchar(30), 
    id varchar(30)
)
insert into @foo values 
('J*SOAP','js@x.com','4321',NULL),
('J*SOAP','js@x.com',NULL,NULL),
('J*SOAP',NULL,'4321','4'),
('J*SOAP','otherjoe@y.com',NULL,'2'),
('J*DOE',NULL,'12345','3')

;with 
merge_by_email as (
    select name, email, MAX(phone) as phone, MAX(id) as id 
    from @foo 
    group by name, email
),
merge_by_phone as (
    select name, max(email) as email, phone, MAX(id) as id 
    from merge_by_email 
    group by name, phone
),
merge_by_id as (
    select name, max(email) as email, MAX(phone) as phone, id 
    from merge_by_phone 
    group by name, id
)
select * from merge_by_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM