Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Concatenating Rows Expand / Collapse
Author
Message
Posted Wednesday, October 14, 2009 3:26 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 22, 2014 8:32 AM
Points: 163, Visits: 428
Grouping by other values, possibly from another table may look like this:
/**
* Test Data
**/
DECLARE @ParentItem TABLE(ParentID INT IDENTITY(1,1) NOT NULL PRIMARY KEY, ParentName NVARCHAR(50), ParentDate DATETIME);
DECLARE @ChildItem TABLE(ChildID INT IDENTITY(1,1) NOT NULL PRIMARY KEY, ParentID INT, ChildName NVARCHAR(50));
INSERT @ParentItem(ParentName,ParentDate)
SELECT 'The first one','01 jan 2009' UNION ALL
SELECT 'The second one','01 feb 2009'
INSERT @ChildItem(ParentID,ChildName)
SELECT 1,'Cat' UNION ALL
SELECT 1,'Dog' UNION ALL
SELECT 1,'Monkey' UNION ALL
SELECT 2,'Teapot' UNION ALL
SELECT 2,'Merkin' UNION ALL
SELECT 2,'Grommet'

/**
* The concatination
**/
SELECT p.ParentID, p.ParentName,
STUFF((SELECT ', ' + ChildName FROM @ChildItem WHERE ParentID = p.ParentID FOR XML PATH('')),1,2,'')
FROM @ParentItem p
GROUP BY p.ParentID, p.ParentName



_______________________________________________________
Change is inevitable... Except from a vending machine.
Post #802590
Posted Wednesday, October 14, 2009 3:33 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, June 20, 2012 5:02 AM
Points: 530, Visits: 945
that's much better!
Post #802597
Posted Wednesday, October 14, 2009 3:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 23, 2012 5:58 AM
Points: 8, Visits: 36
In my opinion the 'neatest' solution is to use a custom CLR aggregate function. Afterall this is an aggregate function. Happily MS provide a concatenate as an example or custom aggregates (first example):
http://technet.microsoft.com/en-us/library/ms131056.aspx

I appreciate that for many DBAs rolling out CLR code is a bit of a culture change but it only needs to be done once and the performance seems to be fine.
Post #802603
Posted Wednesday, October 14, 2009 4:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 26, 2014 5:32 AM
Points: 14, Visits: 130
You're missing several other solutions

3. using UDF, coalesce and a subview

ALTER FUNCTION dbo.Concatenate_Functiebenaming (@ID_Personeelslid INT)
returns varchar(200)
as BEGIN
declare @str varchar(202)
select @str = coalesce(@str, '') + ', ' + Ambt + ISNULL(': '+Functie, '')
from [vw_Personeelsleden_Huidige_Benaming_Sub]
where [ID_Personeelslid] = @ID_Personeelslid
return substring(@str, 3, 200)
END

you call it like this
CREATE VIEW dbo.vw_Personeelsleden_Huidige_Benaming
SELECT DISTINCT ID_Personeelslid, dbo.Concatenate_Functiebenaming(ID_Personeelslid) AS Functiebenaming
FROM dbo.vw_Personeelsleden_Huidige_Benaming_Sub
GROUP BY ID_Personeelslid

4. UDF aggregate function with CLR (google it)

and

hey, here is an overview of almost all (solution 3 above is not there) + comment on drawbacks

http://www.projectdmx.com/tsql/rowconcatenate.aspx
Post #802616
Posted Wednesday, October 14, 2009 5:10 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 22, 2014 8:32 AM
Points: 163, Visits: 428
Joel Mansford (10/14/2009)
In my opinion the 'neatest' solution is to use a custom CLR aggregate function. Afterall this is an aggregate function. Happily MS provide a concatenate as an example or custom aggregates (first example):
http://technet.microsoft.com/en-us/library/ms131056.aspx

I appreciate that for many DBAs rolling out CLR code is a bit of a culture change but it only needs to be done once and the performance seems to be fine.


The other big problem with this is if you have no idea how to actually compile the DLL or do not have the requisite software to do so.

I had a look at the example, and even with the other one showing 2 parameters I was at a complete loss as to how to alter the aggregation to take a parameter for custom separator.


_______________________________________________________
Change is inevitable... Except from a vending machine.
Post #802637
Posted Wednesday, October 14, 2009 5:15 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, June 29, 2014 11:01 AM
Points: 57, Visits: 216
how about this:

declare @FinalString nvarchar(250)

SET @FinalString=''

SELECT @FinalString = COALESCE(@FinalString + ', ', '') + (CAST([name] AS nvarchar(50)))
FROM Fruit
order by [name]


SELECT REPLACE(@FinalString,'''',' ')

Post #802642
Posted Wednesday, October 14, 2009 5:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 23, 2012 5:58 AM
Points: 8, Visits: 36
It's worth learning!

With regard to software I could be wrong but I believe that Visual Studio Express is all that you need. The CLR stuff comes in useful in other areas for instance with Regular Expressions or for making external web service calls.

One hacky way to support changing the separator character is to use something obscure in the code such as '¦' and then use the T-SQL REPLACE function to substitute it as required.

Personally I just have a version for comma (',') and then another that uses semi-colon (';') as that keeps the usage of the function simple.
Post #802644
Posted Wednesday, October 14, 2009 5:22 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 14, 2009 7:20 AM
Points: 1, Visits: 12
and what about PIVOT?
Post #802645
Posted Wednesday, October 14, 2009 5:26 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 30, 2014 2:57 PM
Points: 68, Visits: 408
I was trying this the same way as Koenraad Dendievel using a function i found elsewhere, seems to work a treat with this:

CREATE FUNCTION dbo.ConcatFruit()
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Output VARCHAR(8000)
SELECT @Output = COALESCE(@Output+', ', '') + Name
FROM fruit

RETURN @Output
END
GO

SELECT TOP 1 dbo.ConcatFruit()
FROM fruit
GO
DROP FUNCTION dbo.ConcatFruit
GO
Post #802646
Posted Wednesday, October 14, 2009 6:17 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 12:53 AM
Points: 2,529, Visits: 2,402
declare @l varchar(max)
,@comma varchar(2)
set @l = ''
set @comma = ''
select @l = @l + @comma + name
,@comma = ', '
from
fruit
ORDER BY name

This is the more performant!
Post #802662
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse