Return Multiple Identical Records

Hello,
I need to return multiple records for each record in a table based on the numeric value of one of the columns.  I have this working in a separate sproc (example below) , but it is using non-numeric values to determine number of rows. I'm looking for a more mathy way to use the quantity column to determine the number of rows to return, so I don't have to have a hundred union alls.

Sample data is attached. The confirmeddrops field is the one that determines how many duplicates of the row to return.
C--Users-dcastling-Desktop-Book1.xlsx

	--Return multiple rows based on breakpoint/level.	
	INSERT INTO @Results(program,wAgin,mrkNbr,brand,ofln,bp,oflnAG,oflnCA,oflnCR,oflnGB,oflnTT,oflnWM)
	SELECT program,wAgin,mrkNbr,SUBSTRING(loc,3,2) Brand, SUBSTRING(fill,5,4) Ofln,bp,'','','','','',''
	FROM tbl_lsu
	CROSS APPLY (
		SELECT 1 AS bp_repeater UNION ALL
		SELECT 2 WHERE bp IN ( 'D', 'T', 'Q' ) UNION ALL
		SELECT 3 WHERE bp IN ( 'T', 'Q' ) UNION ALL
		SELECT 4 WHERE bp IN ( 'Q' )
	) AS bp_repeater
	WHERE SUBSTRING(loc,3,2) = @Brand
	AND program = @Prog
	AND progYear = @Year
	AND Convert(int,SUBSTRING(fill,5,4)) BETWEEN @Ofln AND @LOfln

Open in new window


*MS SQL SERVER 2008 R2
coperations07Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brendt HessSenior DBACommented:
The simplest way to do this (and one of the most versatile ways to do a number of different tasks in SQL Server) is to make a tally table - just a table containing a sequence of numbers, of any size.

First, create the table:
CREATE TABLE tally (num int primary key clustered)

Open in new window

Then populate the table. Here is how I usually do this:
DECLARE @ct int, @num int, @max int
INSERT INTO tally (num) values (1);
INSERT INTO tally (num) values (2);

SET @ct = 2
WHILE @ct <= 12  -- this will be big enough for most uses - 2^12 entries
    SELECT @max = max(num) FROM tally

    INSERT INTO tally (num)
    SELECT num + @max
    FROM tally

    Set @ct = @ct + 1
END

Open in new window


Now, to use it to return multiple copies of your data, just JOIN to the tally table. Assuming a simple (value = number of rows) condition, a simple INNER JOIN would do:
INNER JOIN tally t
ON t.num BETWEEN 1 and x.value

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
>>"I need to return multiple records for each record in a table based on the numeric value of one of the columns."
Why?

The tally table suggestion above will certainly solve the specific question, but why are you needing this deliberate repetition?
0
coperations07Author Commented:
It's for a Quality Control check application. Scan checks one by one with a timestamp on each check.

Thanks I'll give the tally a try.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
coperations07, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.