2016-06-01 1 views
-1

後述するように、私はcase文を使用している場合、私は、SQLのCASEの使い方&ピボット

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 

を表Bに

Cust_ID Campaign_Date Campaign_Date1  Campaign_Date2 
1   20160101  20160108    20160115 
2   20160201  20160208    20160215 

を持っていますピボットがあると思って、出力に関してデータにも要求する。

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, 
+5

質問がありますか? –

+0

DBMSはテーブルが存在するオラクルです – las

+0

こんにちはGiorgi、私はかなり大文字と小文字の両方でロジックをコーディングする方法や出力結果を得る他の方法を混乱させています – las

答えて

1

限り(cust_id, Bet_Date_Placed_Key)tableBに一意であるように、ちょうどテーブル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_id, Bet_Date_Placed_Key)ことによってそれをTABLEB量および。

+0

正解Serg – las