sql server - Duplicating rows based on a column value in each row -
i've table following data
job quantity status repeat 1 100 ok 2 2 400 hold 0 3 200 hold 1 4 450 ok 3 based on value in repeat column each row, row should repeated again. example job 1, repeat value 2 job 1 should repeat 2 more times.
the resultant table should below
job quantity status repeat 1 100 ok 2 1 100 ok 2 1 100 ok 2 2 400 hold 0 3 200 hold 1 3 200 hold 1 4 450 ok 3 4 450 ok 3 4 450 ok 3 4 450 ok 3 can please me out query?
i'm using sql server
this support on 7,400 repeats individual job (on system). if need more, can use different system table or cross join.
declare @d table (job int, quantity int, status varchar(12), repeat int); insert @d select 1, 100, 'ok' ,2 union select 2, 400, 'hold',0 union select 3, 200, 'hold',1 union select 4, 450, 'ok' ,3; x ( select top (select max(repeat)+1 @d) rn = row_number() on (order [object_id]) sys.all_columns order [object_id] ) select * x cross join @d d x.rn <= d.repeat + 1 order job;
Comments
Post a Comment