Solved

Concantenate over multiple rows: SQL 2005

Posted on 2006-07-18
4
211 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

747 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

11 Experts available now in Live!

Get 1:1 Help Now