﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / T-SQL Challenge, One row to Many based on Quantity / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 12:52:25 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: T-SQL Challenge, One row to Many based on Quantity</title><link>http://www.sqlservercentral.com/Forums/Topic557570-8-1.aspx</link><description>I agree with Matt Miller.  This is a good candidate for using a tally table.:cool:</description><pubDate>Fri, 22 Aug 2008 13:28:09 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: T-SQL Challenge, One row to Many based on Quantity</title><link>http://www.sqlservercentral.com/Forums/Topic557570-8-1.aspx</link><description>Output would be something like:[code]Item 1, Widget, 2Item 1, Widget, 2Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 2, Junk, 17Item 3, Bars, 9Item 3, Bars, 9Item 3, Bars, 9Item 3, Bars, 9Item 3, Bars, 9Item 3, Bars, 9Item 3, Bars, 9Item 3, Bars, 9Item 3, Bars, 9[/code]We know we could use a temp or existing table with 10000+ rows in it by doing something like this.[code]select * from items i inner join numbers non i.quantity &amp;lt;= n.number[/code]I'm happy with this result and doing it this way.  ANother guy in my office has some kind of weird obsession with not doing it this way and wants use temp tables specifically created with the rows needed.I know in 2005 this is easily done with a CTE or ROW_Number and cross applies but I want to show him a sql 2000 solution.  Consider it a challenge.</description><pubDate>Fri, 22 Aug 2008 13:11:48 GMT</pubDate><dc:creator>john.farrell</dc:creator></item><item><title>RE: T-SQL Challenge, One row to Many based on Quantity</title><link>http://www.sqlservercentral.com/Forums/Topic557570-8-1.aspx</link><description>John - It sounds to me that you want to use a Tally table.  it will allow you to return the desired amount of rows needed.Here's a good primer on how to use it:[url=http://www.sqlservercentral.com/articles/TSQL/62867/]http://www.sqlservercentral.com/articles/TSQL/62867/[/url]</description><pubDate>Fri, 22 Aug 2008 13:03:13 GMT</pubDate><dc:creator>Matt Miller (#4)</dc:creator></item><item><title>RE: T-SQL Challenge, One row to Many based on Quantity</title><link>http://www.sqlservercentral.com/Forums/Topic557570-8-1.aspx</link><description>I'm a visually oriented type, based on the sample data provided what would the output look like?:cool:</description><pubDate>Fri, 22 Aug 2008 12:49:21 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>T-SQL Challenge, One row to Many based on Quantity</title><link>http://www.sqlservercentral.com/Forums/Topic557570-8-1.aspx</link><description>So I have a table of Items, each with a quantity, and a type.[code]CREATE TABLE dbo.Items(	name varchar(50),	type varchar(50),	quantity int)INSERT INTO Items ( name, type, quantity ) VALUES ( 'Item 1', 'Widget', '2' )INSERT INTO Items ( name, type, quantity ) VALUES ( 'Item 2', 'Junk', '17' )INSERT INTO Items ( name, type, quantity ) VALUES ( 'Item 3', 'Bars', '9' )[/code]Now what I need to do is for each quantity in that row, transform that into that number of rows rows.  I know I can do it with a cursor, or an inner join on a temp table, but is there a way, in SQL 2000, to use a Table Function to return a dynamic table with the number of rows in the quantity field? and then join on that to get the rows you need.The challenge is to not use a loop inside the table function, we already have that one figured out.Don't need too much help as we already know some solutions, just looking for the best, set based approach as an interesting academic exercise.</description><pubDate>Fri, 22 Aug 2008 12:42:24 GMT</pubDate><dc:creator>john.farrell</dc:creator></item></channel></rss>