Solved

Help with Sql Select query

Posted on 2015-02-18
7
77 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

831 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