Solved

Concantenate over multiple rows: SQL 2005

Posted on 2006-07-18
4
215 Views
Last Modified: 2008-03-06
Consider this table if you will

X1, X2, Y, Details
US, IT, Catagory1, Manager
US, IT, Catagory1, Sales
US, IT, Catagory2, Programmer

I want to concatenate the details of rows with the same X1, X2 and Y
So the result should be

US, IT, Catagory1, Manager + Sales
US, IT, Catagory2, Programmer

What would the SQL or stored procedure look like to pull this off optmized for SQLserver 2005?
0
Comment
Question by:DaFou
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 300 total points
ID: 17135211
Hi DaFou,

create a function like this

CREATE function dbo.RetCSVs (
@x1 varchar(100),
@x2 varchar(100),
@Y  varchar(100)
)
Returns Varchar(8000)
AS
BEGIN
    declare @out Varchar(8000)
    SELECT @out = COALESCE (RTRIM(@out)+',','')+Details
    FROM urTable ------------ replace  this with ur table name
    WHERE X1 = @x1 AND  X2 = @x2 AND Y =@y

    return @out
END
GO

And call like

SELECT X1,X2,Y, dbo.RetCSVs(X1,X2,Y) As DesiredColumnName
FROM urTable



Aneesh R!
0
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 200 total points
ID: 17135517
You might want to make the last call:
SELECT X1,X2,Y, dbo.RetCSVs(X1,X2,Y) As DesiredColumnName
FROM urTable
GROUP BY X1,X2,Y
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17135527
Thanks DireOrbAnt

OR ELse

SELECT DISTINCT X1,X2,Y, dbo.RetCSVs(X1,X2,Y) As DesiredColumnName
FROM urTable
0
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 200 total points
ID: 17141795
>>OR ELse SELECT DISTINCT
I would not do that, because SQL doesn't know that dbo.RetCSVs(X1,X2,Y) will return the same value for each rows. The DISTINCT will filter AFTER running all those calls, the group by will allow the RS to be filtered before.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 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

14 Experts available now in Live!

Get 1:1 Help Now