Solved

Multiple Rows into a Single Row

Posted on 2006-10-26
3
369 Views
Last Modified: 2008-03-10
I am trying to join Multiple Rows into a single Row for this stored procedure, so that it will look like the following:

Parameter       Price         Method

Copper            $11            200.8, 6020, 345.1
Arsenic            $11            200.8, 6020

For each unique combination of Parameter and Price. The Method table of course has multiple entries for each parameter, and usually at the same price.

Here is my stored procedure so far:

CREATE PROCEDURE [sp_PriceList2]
AS
SET NOCOUNT ON
DECLARE @price            DECIMAL(7, 2)
DECLARE @prmid            INTEGER
DECLARE @mthdtmp            NVARCHAR(30)
DECLARE @mthdrow            NVARCHAR(200)
DECLARE @my_cur CURSOR
DECLARE @my_cur2 CURSOR
SELECT @mthdrow = ''
SET @my_cur = CURSOR FOR
      SELECT DISTINCT PRM_ID,  MTHD_PRICE FROM PARAMETERS INNER JOIN METHODS ON MTHD_PRMID = PRM_ID
      WHERE MTHD_PRICE > 0

OPEN @my_cur
FETCH NEXT FROM @my_cur INTO @prmid, @price
WHILE @@FETCH_STATUS = 0
BEGIN
      SET @my_cur2 = CURSOR FOR
      SELECT MTHD_REF FROM METHODS WHERE MTHD_PRMID = @prmid AND MTHD_PRICE = @price
      OPEN @my_cur2
      FETCH NEXT FROM @my_cur2 INTO @mthdtmp
      WHILE @@FETCH_STATUS = 0
            BEGIN
                  SELECT @mthdrow = @mthdrow + @mthdtmp + ','
                  FETCH NEXT FROM @my_cur2 INTO @mthdtmp
            END
            CLOSE @my_cur2
            DEALLOCATE @my_cur2
            SELECT DISTINCT PRM_ID, PRM_ABBREV, MTHD_PRICE, @mthdrow FROM PARAMETERS INNER JOIN METHODS ON MTHD_PRMID = PRM_ID
            WHERE PRM_ID = @prmid AND MTHD_PRICE = @price

            SELECT @mthdrow = ' '

FETCH NEXT FROM @my_cur INTO @prmid, @price
END
CLOSE @my_cur
DEALLOCATE @my_cur
GO

This almost does what I want it to. It actually returns about 290 different single line tables. I need someway to merge all those together, and someway to tell when I am on my last Fetch into Cursor 2, so that I can drop that last comma off.


0
Comment
Question by:Xeavn
[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
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
rw3admin earned 500 total points
ID: 17815372
Try this
Asssuming you have a table called MyTable as follows

Parameter       Price         Method

Copper            $11            200.8
Copper            $11            6020,
Copper            $11            345.1
Arsenic           $11            200.8
Arsenic           $11            6020

create table #T(Id int identity(1,1),Parameter Varchar(255), price Varchar(255))

Insert #T (Parameter, Price)
select distinct Parameter, Price from MyTable

create table #Final(Parameter Varchar(255), price Varchar(255), Method varchar(8000))
Declare
@ID      int,
@Method Varchar(1000)

Select @ID=1, @Method=''
While @ID<=(Select Max(ID) from #T))
Begin
Select      @Method=@Method+','Method
From      MyTable
Inner Join
      #T
ON      #T.Parameter=MyTable.Parameter and      
      #T.Price=MyTable.Price
Where      #T.ID=@ID

INsert #Final
Select      Parameter,
      price,
      @Method
From      #T
Where      #T.ID=@ID
Select @Method=Left(@Method, Len(@Method)-1)

Select @ID=@ID+1, @Method=''
End

Select * from #Final
0
 
LVL 4

Author Comment

by:Xeavn
ID: 17815649
Thanks rw3admin,

It wasn't completely what I was after, but it was close enough that I was able to figure the rest out on my own.
0
 
LVL 11

Expert Comment

by:rw3admin
ID: 17815689
Thanks,
Yes I didnt mess with your code I just wanted to give you an example, I knew you were intelligent enough to figure how to make it work in your own code.

rw3admin
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

688 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