有一张表,两个字段id和num,如何将这张表的数据行数增加为原来的4倍,值按行翻倍@
表 test
id a1
1 50
2 30
3 40
韬哥赐给你一段机缘吧
select test.id,
(test.a1*(4-(if(bb1=1,1,0)+if(bb2=1,1,0)+if(bb3=1,1,0)))) as aa1
from
test
,(select 1 as bb1) as b1,(select 1 as bb2) as b2,(select 1 as bb3) as b3
group by test.id,bb1,bb2,bb3 with ROLLUP
having test.id is not null
结果
1 50
1 100
1 150
1 200
2 30
2 60
2 90
2 120
3 40
3 80
3 120
3 160
直接UNION ALL四次数据不就翻4倍了
用临时表呢
CTE递归,控制深度,然后窗口累积num
WITH RECURSIVE CTE as (
select id, num , 1 as c from example union all
select id, num , c + 1 from CTE where c < 4
) select id, sum(num) over(partition by id rows between unbounded preceding and current row) num
from CTE order by id
游标循环插入可以不?