SQL用例用法&;支点
在表A中,我有SQL用例用法&;支点,sql,oracle,Sql,Oracle,在表A中,我有 Cust_ID Campaign_Date Campaign_Date1 Campaign_Date2 1 20160101 20160108 20160115 2 20160201 20160208 20160215 在表B中 Cust_ID Bet_Date_Placed_Key Amount1 Amount2 Amount3 1
Cust_ID Campaign_Date Campaign_Date1 Campaign_Date2
1 20160101 20160108 20160115
2 20160201 20160208 20160215
在表B中
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
输出
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
如果我使用下面提到的case语句,那么我认为还需要一个关于输出的数据透视
select cust_id,
campaign_date,
(case when campaign_date=Bet_Date_Placed_Key then amount1 else null end) as Amount1,
(case when campaign_date=Bet_Date_Placed_Key then amount2 else null end) as Amount2,
(case when campaign_date=Bet_Date_Placed_Key then amount3 else null end) as Amount3,
只要(客户id、下注日期、下注键)
在表B中是唯一的,只需加入表B 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
否则,首先通过(cust\u id,Bet\u Date\u Placed\u Key)
将其金额聚合到表B中,然后以相同的方式加入聚合结果集。您有什么问题吗?DBMS是oracle,表在其上呈现,我很困惑如何用case和pivot来编写逻辑代码,或者用任何其他方法来生成输出,你到底遇到了什么问题?不清楚你在问什么Hi Pablomatico,Sergo的答案是正确的,并且解决了问题正确的答案Serg