Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Repeating rows from existing table

Author  Topic 

sqlkid
Starting Member

41 Posts

Posted - 2012-03-07 : 06:23:12
I have data similar to what is attached with a column for 'count'. I need to create a new table or replace the existing table where the row gets repeated "n" times where n is the count in the original table. I am using SQL 2000 - what command can I give to get the required output.

[URL=http://imageshack.us/photo/my-images/3/dataformat.png/][/URL]

Uploaded with [URL=http://imageshack.us]ImageShack.us[/URL]

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-07 : 06:49:31
[code]
select *
from Data d
cross join NumberTable n
where n.number <= d.Count
[/code]

NumberTable is just a table with numbers like 1, 2, 3 etc
alternatively you can use master..spt_values
or F_TABLE_NUMBER_RANGE http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685&SearchTerms=F_TABLE_NUMBER_RANGE


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -