Suppose I have a set in SQL like this:
Product | Quantity
A 1
B 2
I want (in a single SELECT statement) to transform that to:
Product
A
B
B
Can anyone point me towards (T-SQL preferably), a trick on how to do this?
You need an intermediary numbers table, or a table-valued function (if that option is available to you) which will produce numbers.
Assuming you had a Numbers table, which is populated like so:
(or as large as you need it to be, and there are efficient mechanisms for generating a numbers table of large size)
You would then issue the following query:
This would produce the result you want.
A numbers table is incredibly helpful in SQL, and Itzik Ben-Gan goes into it and other great querying techniques in his books (listed on his website). I highly recommend them.