您好,我需要合并2个表格与关于客户的信息。表2告诉我们,如果我们有关于电子邮件,地址和电话号码的客户信息,但是数据的结构使每个客户有3行。有没有合并这两个表的方法,以便我只为每个客户获得一行,但包含所有联系信息?几行信息到单行
表1:
id customerID ... ...
1 11
2 99
和表2:
id customerID Channel Y_N
1 11 Email Y
2 11 Address Y
3 11 Phone N
4 99 Email N
5 99 Address Y
6 99 Phone N
我的代码是这样的
TABLE 1
left join(
select customerID,
case when Y_N='Y' and Channel='Email' then 1 else 0 end as Email
FROM table2
where Channel='Email')a
on table1.customerID=a.customerID
Left join(
select customerID,
case when Y_N='Y' and Channel='Address' then 1 else 0 end as Address
FROM table2
where Channel='Address') b
on table1.customerID=b.customerID
Left join(
select customerID,
case when Y_N='Y' and Channel='Phone' then 1 else 0 end as Phone
FROM table2
where Channel='SMS') c
on table1.customerID=c.customerID
实际上做的工作,但如果我要再做一次在未来有没有更聪明的方法?
谢谢
我不负责创建的数据库,我也宁愿把它构造为你展示它:)谢谢 – IDDQD