Solved

How to collapse rows returned from SQL SELECT into one?

Posted on 2004-04-06
2
459 Views
Last Modified: 2008-02-20
Hello,

the following query joins two tables and produces the following results:

SELECT DT1.Name, DT1.SectorID, DT1.ExposureBiasID, DT2.AssetClassID
FROM DT_Strategy DT1, DT_StrategyAssetClass DT2
WHERE DT1.ID = DT2.StrategyID AND DT1.ID = 1

------------------------------------------------------------------
Relative Value Equity Long      4      1      1
Relative Value Equity Long      4      1      2
Relative Value Equity Long      4      1      3
------------------------------------------------------------------

All the resulting rows are equal except for the value in the last column. I would like to collapse them into one row as follows:

----------------------------------------------------------------------
Relative Value Equity Long      4      1      1,2,3
----------------------------------------------------------------------

Is this possible in SQL? Or do I need to do it programatically?

Thanks,
Stefano
0
Comment
Question by:sdamiano
[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 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 10770277
On SQL 2000, you can use a user-defined function.  For example:


CREATE FUNCTION dbo.getAssetClassIDs (
      @ID INT
)
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE @AssetClassIDs VARCHAR(1000)
SET @AssetClassIDs = ''
SELECT @AssetClassIDs = @AssetClassIDs + CAST(AssetClassID AS VARCHAR(10)) + ','
FROM DT_StrategyAssetClass
WHERE StrategyID = @ID
RETURN LEFT(@AssetClassIDs, LEN(@AssetClassIDs) - 1)
END --FUNCTION
GO


Then, to use the function:

SELECT DT1.Name, DT1.SectorID, DT1.ExposureBiasID, dbo.getAssetClassIDs(DT1.ID)
FROM DT_Strategy DT1
WHERE DT1.ID = 1
0
 

Author Comment

by:sdamiano
ID: 10770600
Scott,

it works perfectly!

Thanks,
Stefano
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.

752 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