T-SQL Challenge, One row to Many based on Quantity

  • So I have a table of Items, each with a quantity, and a type.

    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' )

    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.

  • I'm a visually oriented type, based on the sample data provided what would the output look like?

    😎

  • 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:

    http://www.sqlservercentral.com/articles/TSQL/62867/[/url]

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Output would be something like:

    Item 1, Widget, 2

    Item 1, Widget, 2

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 2, Junk, 17

    Item 3, Bars, 9

    Item 3, Bars, 9

    Item 3, Bars, 9

    Item 3, Bars, 9

    Item 3, Bars, 9

    Item 3, Bars, 9

    Item 3, Bars, 9

    Item 3, Bars, 9

    Item 3, Bars, 9

    We know we could use a temp or existing table with 10000+ rows in it by doing something like this.

    select * from items i inner join numbers n

    on i.quantity <= n.number

    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.

  • I agree with Matt Miller. This is a good candidate for using a tally table.

    😎

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply