Banthor
asked on
Expanding QTYxItem to row of unique ITEMs
I have a OrderedITems table (rowid [int],Itemid [int],qty[smallint])
I need to insert into the manifest one row for each item as expressed by Qty
So if OrderedItems contains QTY 6 of Item 4
My select needs to return 6 rows of ITEMID 4
Does someone have a single select statement solution?
I need to insert into the manifest one row for each item as expressed by Qty
So if OrderedItems contains QTY 6 of Item 4
My select needs to return 6 rows of ITEMID 4
Does someone have a single select statement solution?
Here is an example using a temp table. It will work for unlimited qty...
create table #temp (
rowID INT
, itemID INT
)
DECLARE @loop INT
SET @loop = 1
WHILE @loop <= (SELECT MAX(qty) FROM OrderedITems)
BEGIN
INSERT INTO #temp SELECT rowID, itemID FROM OrderedITems WHERE qty >= @loop
SET @loop = @loop + 1
END
SELECT * FROM #temp ORDER BY rowID, itemID
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cross Join is not something I use a lot, but this work great. I should look into using a CTE so that I am not limited to the value of spt_values
ASKER
Actually CTE's are limited to recursion limit of 100
>>Actually CTE's are limited to recursion limit of 100 <<
I suspect you mean:
Actually CTE's are limited to a default recursion limit of 100
The actual max limit is 32,767. See MAXRECURSION hint.
I suspect you mean:
Actually CTE's are limited to a default recursion limit of 100
The actual max limit is 32,767. See MAXRECURSION hint.
Actually even that is not true, if you are brave you can set it to 0.
Open in new window