首页 > 数据库 > Oracle > 巧用笛卡尔积一行拆多行
需求:
将表中每行数据按第二列的值进行拆分,值为n则拆分为n行,每行值取1.
--巧用笛卡尔积一行拆多行
--笛卡尔积+过滤
with ta as
(select 'a' id, 2 cnt
from dual
union all
select 'b' id, 1 cnt
from dual
union all
select 'c' id, 3 cnt
from dual
union all
select 'd' id, 1 cnt
from dual),
tb as
(select level cnt
from dual
connect by level <= 7)
select a.cnt,b.cnt,a.id, 1 from ta a, tb b
where a.cnt >= b.cnt order by 1, 2;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/18922393/viewspace-1318523/ ,如需转载,请注明出处,否则将追究法律责任。