Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add variable number of rows into a table based on the values in another table (without cursors/while...


Add variable number of rows into a table based on the values in another table (without cursors/while loops)

Author
Message
charles99
charles99
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 175
Below is a simplified version of my tables:

set nocount on
drop table #x
create table #x (docid int, pages int)
insert into #x values (1, 1)
insert into #x values (2, 5)
insert into #x values (3, 2)
insert into #x values (4, 3)

select * from #x;

drop table #y
create table #y (docid int, pagenumber int)
insert into #y values (1, 1)
insert into #y values (2, 1)
insert into #y values (2, 2)
insert into #y values (2, 3)
insert into #y values (2, 4)
insert into #y values (2, 5)
insert into #y values (3, 1)
insert into #y values (3, 2)
insert into #y values (4, 1)
insert into #y values (4, 2)
insert into #y values (4, 3)

select * from #y;

set nocount off

So basically I have an input table #x with a docid and total number of pages within that docid.
How can I construct the output table #y that has n rows per #x.docid where n is #x.pages?
I can do it with cursors or while loops etc in a few different ways (either per docid or one insert/select per distinct #x.pages value)

I am wondering if there is a set based T-SQL solution to this? Can CTEs be somehow used for this?
I am creating code ultimately for SQL Server 2008 R2 enterprise edition.

Any help is greatly appreciated!

Thanks.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16671 Visits: 17030
Sure this isn't too hard using a tally table.


insert #y
select #x.DocID, t.N
from #x
join tally t on t.N <= #x.pages

select * from #y



You can read about a tally table here. http://www.sqlservercentral.com/articles/T-SQL/62867/

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24280 Visits: 37987
Sean Lange (3/20/2013)
Sure this isn't too hard using a tally table.


insert #y
select #x.DocID, t.N
from #x
join tally t on t.N <= #x.pages

select * from #y



You can read about a tally table here. http://www.sqlservercentral.com/articles/T-SQL/62867/


I'm starting to see a pattern with some of the posts. Looks like people are coming here to get their homework done. Anyone else seeing this?

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)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45442 Visits: 39942
Lynn Pettis (3/20/2013)
Sean Lange (3/20/2013)
Sure this isn't too hard using a tally table.


insert #y
select #x.DocID, t.N
from #x
join tally t on t.N <= #x.pages

select * from #y



You can read about a tally table here. http://www.sqlservercentral.com/articles/T-SQL/62867/


I'm starting to see a pattern with some of the posts. Looks like people are coming here to get their homework done. Anyone else seeing this?


Yep... I'm thinking the instructor is going to make a mess in his britches when everyone turns in the Tally Table solution. Hopefully, the instructor learns something in the process. :-P

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16671 Visits: 17030
Jeff Moden (3/20/2013)
Lynn Pettis (3/20/2013)
Sean Lange (3/20/2013)
Sure this isn't too hard using a tally table.


insert #y
select #x.DocID, t.N
from #x
join tally t on t.N <= #x.pages

select * from #y



You can read about a tally table here. http://www.sqlservercentral.com/articles/T-SQL/62867/


I'm starting to see a pattern with some of the posts. Looks like people are coming here to get their homework done. Anyone else seeing this?


Yep... I'm thinking the instructor is going to make a mess in his britches when everyone turns in the Tally Table solution. Hopefully, the instructor learns something in the process. :-P


LOL. I guess my "homework meter" has been temporarily disabled. It should be back online in the next few minutes.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
charles99
charles99
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 175
Thank you Sir! Not sure how to interpret the other replies and so I will leave it at that. The article was really helpful. Thank you again.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16671 Visits: 17030
charles99 (3/20/2013)
Thank you Sir! Not sure how to interpret the other replies and so I will leave it at that. The article was really helpful. Thank you again.


You are welcome.

The other replies are in response to some people that come here and post their homework questions. There is nothing wrong with that per se but if you are a student then it is a disservice by myself or any other posters to simply provide the answer. Sort of the old "teach a man to fish" concept.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
charles99
charles99
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 175
I see ... I wish I were still a student. The pros are making me nervous asking my next "real-world" question on this site again! Smile
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16671 Visits: 17030
charles99 (3/20/2013)
I see ... I wish I were still a student. The pros are making me nervous asking my next "real-world" question on this site again! Smile


Don't worry about it. There was something in the way you had worded your post that made it sound as though it might be a homework type question. Ask away, somebody will come along and help.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24280 Visits: 37987
charles99 (3/20/2013)
I see ... I wish I were still a student. The pros are making me nervous asking my next "real-world" question on this site again! Smile


The question you asked has properties very similar to several questions asked by others, so it really is just the pattern that set off a "possible homework" alarm. We are more than willing to help. If we know it is homework, we tend to take a more hands off approach (or at least try to) and try to guide instead just answering the question.

We want people to understand what they are doing. For real world questions, we'll answer than normal work backward on the understanding side because you are still the one who will get the call at 3:30 in the morning when things break, not us.

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