Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Concantenate over multiple rows: SQL 2005

Posted on 2006-07-18
4
Medium Priority
?
221 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
[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
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1200 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 800 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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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