我有三张桌子:
Email | EmailID
jack@gmail.com | 100
nate@gmail.com | 101
rex@gmail.com | 102
第二
Email | CustomerID
jack@gmail.com | 90001
jack@gmail.com | 90002
nate@gmail.com | 90003
rex@gmail.com | 90003
第三
CustomerID | Purchases
90001 | 1
90002 | 2
90003 | 5
如您所见,Jack有2个与他关联的客户ID,而客户ID 90003有两个与之关联的电子邮件。我想正确地重复删除客户并给杰克1个电子邮件地址和1个客户ID(无论哪个都没关系。)
所以,理想情况下,在总结购买时我会得到以下信息:
Email | EmailID |CustomerID | Purchases
jack@gmail.com | 100 |90001 | 3
nate@gmail.com | 101 |90003 | 5
nate@gmail.com | 102 |90003 | 5
答案 0 :(得分:0)
我很困惑。得到你要求的结果将是这样的。
Name = "John"
_margin = "whatever"
答案 1 :(得分:0)
我的解决方案,假设结果中有拼写错误
初始化:
create table #table1 (email varchar(100), emailid int)
insert into #table1 values ('jack@gmail.com', 100), ('nate@gmail.com', 101), ('rex@gmail.com',102)
create table #table2 (email varchar(100), customerid int)
insert into #table2 values ('jack@gmail.com', 90001), ('jack@gmail.com', 90002), ('nate@gmail.com',90003), ('rex@gmail.com',90003)
create table #table3 (customerid int, purchases int)
insert into #table3 values (90001, 3), (90002, 5), (90003, 5)
查询
select T1.email,
(select max(emailid) from #table1 where email = T1.email) as 'emailid',
(select max(customerid) from #table2 where email = T1.email) as 'customerid',
(sum(T3.purchases)) as 'purchases'
from #table1 T1
right join #table2 T2
on T1.email = T2.email
right join #table3 T3
on T2.customerid = T3.customerid
group by T1.email