在表A中,我有
m
在表B中,
Cust_ID Campaign_Date Campaign_Date1 Campaign_Date2
1 20160101 20160108 20160115
2 20160201 20160208 20160215
输出
Cust_ID Bet_Date_Placed_Key Amount1 Amount2 Amount3
1 20160101 3 4 6
1 20160108 4 5 7
1 20160115 3 4 6
2 20160201 3 4 6
2 20160208 4 5 7
2 20160215 3 4 6
如果我使用下面提到的case语句,那么认为有关输出的数据也需要一个数据透视
Cust_ID Campaign_Date Amount1 Amount2 Amount3 Campaign_Date1 Amount1 Amount2 Amount3 Campaign_Date2 Amount1 Amount2 Amount3
1 20160101 3 4 6 20160108 4 5 7 20160115 3 4 6
2 20160201 3 4 6 20160208 4 5 7 20160215 3 4 6
答案 0 :(得分:1)
至于(cust_id, Bet_Date_Placed_Key)
中的tableB
是唯一的,只需加入TableB 3次
select a.cust_id
, a.campaign_date, b1.Amount1, b1.Amount2, b1.Amount3
, a.campaign_date1, b2.Amount1, b2.Amount2, b2.Amount3
, a.campaign_date2, b3.Amount1, b3.Amount2, b3.Amount3
from tableA a
left join tableB b1 on b1.cust_id=a.cust_id and b1.Bet_Date_Placed_Key = a.campaign_date
left join tableB b2 on b2.cust_id=a.cust_id and b2.Bet_Date_Placed_Key = a.campaign_date1
left join tableB b3 on b3.cust_id=a.cust_id and b3.Bet_Date_Placed_Key = a.campaign_date2
否则,首先聚合按表{B} (cust_id, Bet_Date_Placed_Key)
计算它,然后以相同的方式加入聚合结果集。