Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with Sql Select query

Posted on 2015-02-18
7
Medium Priority
?
92 Views
Last Modified: 2015-02-18
select [lineno], part, qty  from orderslines where rec='S000001' order by [lineno]
produces
lineno      part      qty
1      10021      5.00
2      10370      2.00
3      10405      1.00

What I really want is multiple rows based upon the value in the qty column so the above would look like
1      10021      1.00    --- (5)
1      10021      1.00
1      10021      1.00
1      10021      1.00
1      10021      1.00

2      10370      1.00    --(2)
2      10370      1.00

3      10405      1.00  --(1)
0
Comment
Question by:bensonwalker
  • 3
  • 3
7 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40616599
Which version of SQL Server?
0
 
LVL 1

Author Comment

by:bensonwalker
ID: 40616607
2008 r2
0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 40616636
You can do it this way with a couple loops and  temp tables
DECLARE @orderslines Table (
	ID int identity,
	[lineno] int,
	part varchar(50),
	qty int
	)
INSERT INTO @orderslines ([lineno], part, qty)
SELECT 1, '10021', 5

INSERT INTO @orderslines ([lineno], part, qty)
SELECT 2, '10370', 2

INSERT INTO @orderslines ([lineno], part, qty)
SELECT 1, '10405', 1

SELECT *
FROM @orderslines

DECLARE @LoopCounter int
SELECT @LoopCounter = MIN(ID) FROM @orderslines
DECLARE @LineCount int
DECLARE @LoopCounter2 int

DECLARE @Lines Table (
	[lineno] int,
	part varchar(50)
	)

WHILE @LoopCounter <= (SELECT MAX(ID) FROM @orderslines)
	BEGIN
			SELECT @LineCount = qty
			FROM @orderslines
			WHERE ID = @LoopCounter
	
			SET @LoopCounter2 = 1			
			WHILE @LoopCounter2 <= @LineCount
				BEGIN
					INSERT INTO @Lines ([lineno], part)
					SELECT [lineno], part
					FROM @orderslines
					WHERE ID = @LoopCounter

					SET @LoopCounter2 = @LoopCounter2 + 1

					IF @LoopCounter2 <= @LineCount	
						CONTINUE
					ELSE
						BREAK
				END
		SET @LoopCounter = @LoopCounter + 1
		
		IF @LoopCounter <= (SELECT MAX(ID) FROM @orderslines)
			Continue
		ELSE
			BREAK
	END
SELECT L.[lineno], L.part, 1
FROM @Lines L

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40616637
with mynumbers as
(select row_Number() over(order by (select null)) as mynumber from sys.objects),
mytable as
(Select [lineno], part, 1 as myQty, row_number() over(partition by [lineno] order by (select null)) as myline
from orderslines 
cross join mynumbers
where rec='S000001' )
Select [lineno], part, myQty
from mytable
where myline <= myQty

Open in new window

0
 
LVL 1

Author Comment

by:bensonwalker
ID: 40616646
the above only returns one line for each part. If the part has a qty of 5 I need 5 lines not one.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 total points
ID: 40616656
Oops. Try this:

with mynumbers as
(select row_Number() over(order by (select null)) as mynumber from sys.objects),
mytable as
(Select [lineno], part, Qty, row_number() over(partition by [lineno] order by (select null)) as myline
from orderslines 
cross join mynumbers
where rec='S000001' )
Select [lineno], part, 1 as myQty
from mytable
where myline <= Qty

Open in new window

0
 
LVL 1

Author Closing Comment

by:bensonwalker
ID: 40616659
Perfect just what I was looking for
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question