Solved

Help with Sql Select query

Posted on 2015-02-18
7
73 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
Comment Utility
Which version of SQL Server?
0
 
LVL 1

Author Comment

by:bensonwalker
Comment Utility
2008 r2
0
 
LVL 18

Expert Comment

by:UnifiedIS
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect just what I was looking for
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now