Solved

Help with Sql Select query

Posted on 2015-02-18
7
78 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 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