记录拆分
表1:
----------------
姓名数量
a2
b1
c3
表2:
-----------------
姓名数量
a1
a1
b1
c1
c1
c1
需要从表1生成表2,表2事先不存在。表2的规律为,将表1的数量拆分为以1为数量的记录条数,比如表1中a只为2,则表2中生成2行a值,b为1则表2中b只有1行,c为3则c有3行,求SQL语句,谢谢!
[解决办法]
好象有点难度,先顶后想。
[解决办法]
用游标拆分,while插入表
[解决办法]
。。。
最勺的办法。。。
尽量不用这种了```
[解决办法]
用个临时表
select top 1000 IDENTITY(int,1,1) as id into # from sysobjects a,sysobjects bselect 姓名,1 as 数量into 表2from 表1 a,# twhere a.数量>=t.iddrop table #
[解决办法]
Haiwer的好办法,虽然有些局限,但思路不错。赞一个!
[解决办法]
非常感谢!楼上2位。
[解决办法]
select 姓名,1 as 数量into 表2from 表1 a,(select t1.id+t2.id+t3.id+t4.id+t5.id+t6.id+t7.id+t8.id+t9.id+t10.id+1 from (select 0 as id union all select 1) t1, (select 0 as id union all select 2) t2, (select 0 as id union all select 4) t3, (select 0 as id union all select 8) t4, (select 0 as id union all select 16) t5, (select 0 as id union all select 32) t6, (select 0 as id union all select 64) t7, (select 0 as id union all select 128) t8, (select 0 as id union all select 256) t9, (select 0 as id union all select 512) t10 ) ) twhere a.数量>=t.id