求Sql话语将一行拆分成多行
求Sql语句将一行拆分成多行
有表resource字段如下:
BeginResId、EndResId、count、price
假设有如下数据:
10001 10005 5 100
10007 10012 6 200
通过sql拆分成如下数据
10001 100
10002 100
......
10005 100
10007 200
10008 200
......
10012 200
即将前面区间表示的数据,拆分成非区间表示。
------解决方案--------------------
有表resource字段如下:
BeginResId、EndResId、count、price
假设有如下数据:
10001 10005 5 100
10007 10012 6 200
通过sql拆分成如下数据
10001 100
10002 100
......
10005 100
10007 200
10008 200
......
10012 200
即将前面区间表示的数据,拆分成非区间表示。
------解决方案--------------------
- SQL code
with t1 as ( select 10001 c1,10005 c2,5 c3,100 c4 from dual union all select 10007 c1,10012 c2,6 c3,200 c4 from dual ) select distinct c1+level-1 c1,c4 from t1 connect by level <= c2 - c1 + 1 order by c1 c1 c4 --------------------------- 1 10001 100 2 10002 100 3 10003 100 4 10004 100 5 10005 100 6 10007 200 7 10008 200 8 10009 200 9 10010 200 10 10011 200 11 10012 200
------解决方案--------------------
可以这样实现
- SQL code
select distinct rt.beginresid - 1 + level, rt.price from resource_tab rt connect by level < rt.count + 1
------解决方案--------------------
- SQL code
with cte as( select '10001' BeginResId, '10005' EndResId, 5 count, 100 price from dual union all select '10007', '10012', 6, 200 from dual ) select beginresid+level-1 resid,price from cte connect by level<=count and prior price=price and prior dbms_random.value is not null; 结果为: RESID PRICE ---------------------- ---------------------- 10001 100 10002 100 10003 100 10004 100 10005 100 10007 200 10008 200 10009 200 10010 200 10011 200 10012 200 11 rows selected