Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Sean_B
Posting Yak Master
111 Posts |
Posted - 2009-02-04 : 05:45:23
|
| Hi,I want to group the following dataCREATE TABLE test(a VARCHAR(5),b VARCHAR(5),c VARCHAR(5))INSERT INTO test(a,b,c)SELECT '1','x','5' UNIONSELECT '1','y','6' UNIONSELECT '1','z','7' UNIONSELECT '2','a','8' to get the following results1,x y z,5 6 72,a,8I've done thiscreate function dbo.concat(@a int)returns varchar(1000)asbegin DECLARE @Output VARCHAR(4000) SET @Output = '' SELECT @Output = CASE @Output WHEN '' THEN b ELSE @Output + ' ' + b END from test where a = @a ORDER BY b RETURN @Output endcreate function dbo.concat2(@a int)returns varchar(1000)asbegin DECLARE @Output VARCHAR(4000) SET @Output = '' SELECT @Output = CASE @Output WHEN '' THEN c ELSE @Output + ' ' + c END from test where a = @a ORDER BY c RETURN @Output endSELECT distinct a, dbo.concat(a),dbo.concat2(a) FROM testHowever that is not very nice, I'm sure there is a more generic way of doing this.Any help would be appreciated.Sean |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
Sean_B
Posting Yak Master
111 Posts |
Posted - 2009-02-04 : 07:49:41
|
| Brilliant, thanks.Sean |
 |
|
|
|
|
|
|
|