I am trying to create a query from my database which will expand each row to multiple rows based on a count column. What I need is to take my table:
[DATE] [COUNT] [OTHER_COLUMN]
20120101 3 X
20120201 2 Y
And create this:
[DATE] [OTHER_COLUMN]
20120101 X
20120101 X
20120101 X
20120201 Y
20120201 Y
I have found lots of ways to do the opposite (merge multiple rows into one), or to expand one row with multiple columns into multiple rows (one based on each column), but I can’t seem to find a way to create identical rows. I am using SQL Server 2012.
For this you need a tally table like this one: http://www.sqlmag.com/article/sql-server/virtual-auxiliary-table-of-numbers
Then you can just write:
Here is a SQL-Fiddle to show the solution in action: http://sqlfiddle.com/#!6/2edda/2