2017-05-18 10 views
0

に脱凝集私はばらばらまたはデアグリゲートする必要があり、次の表があります。単一ユニットレベル

Order表を:

単一ユニットのレベルのレコードに上記のデータを爆発するSQLを入力してください以下に示すよう:

所望の出力:

Order Id Item Qty 
O1   A1  1 
O1   A1  1 
O1   A1  1 
O1   A1  1 
O1   A1  1 
O2   A2  1 
O3   A3  1 
O3   A3  1 
O3   A3  1 

Teradataでどうすればいいですか?

答えて

2

あなたがon n between 1 and Qty製品が得られるが、に参加)またはEXPAND ONを利用用いて1からnまでの番号をテーブルに参加することができ、次のいずれか

select OrderId, Item, 1 
from tab 
expand on period (current_date, current_date + Qty) as pd 
0

再帰CTEを有するアプローチ。

with recursive cte(orderid,item,qty,val) as 
     (select orderid, item, qty, 1 
     from t --this is your tablename 
     union all 
     select c.orderid, c.item, c.qty-1, c.val 
     from t 
     join cte c on t.orderid=c.orderid and t.item=c.item and c.qty>1 
     ) 
select orderid,item,val as qty 
from cte 
order by 1,2 

や数字テーブルを持つ(=あなたが持つことができる最大量を、すべての数字を生成する)

with recursive cte(num) as (select 1 num 
          union all 
          select num+1 from cte where num < 1000 --change this to max quantity 
          ) 
select orderid, item, 1 as qty 
from t 
join cte c on t.qty >= c.num 
order by 1,2