Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Concantenate over multiple rows: SQL 2005

Posted on 2006-07-18
4
Medium Priority
?
222 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

886 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