Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

T-SQL Challenge, One row to Many based on Quantity Expand / Collapse
Author
Message
Posted Friday, August 22, 2008 12:42 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 11, 2009 9:13 AM
Points: 11, Visits: 24
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.
Post #557570
Posted Friday, August 22, 2008 12:49 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 1:09 PM
Points: 23,253, Visits: 31,946
I'm a visually oriented type, based on the sample data provided what would the output look like?




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #557573
Posted Friday, August 22, 2008 1:03 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 7,135, Visits: 15,154
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/


----------------------------------------------------------------------------------
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?
Post #557578
Posted Friday, August 22, 2008 1:11 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 11, 2009 9:13 AM
Points: 11, Visits: 24
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.
Post #557583
Posted Friday, August 22, 2008 1:28 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 1:09 PM
Points: 23,253, Visits: 31,946
I agree with Matt Miller. This is a good candidate for using a tally table.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #557592
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse