SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
john.farrell
john.farrell
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 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.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90132 Visits: 38944
I'm a visually oriented type, based on the sample data provided what would the output look like?

Cool

Cool
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)
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27944 Visits: 18995
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?
john.farrell
john.farrell
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 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.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90132 Visits: 38944
I agree with Matt Miller. This is a good candidate for using a tally table.

Cool

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search