![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
Hi Bart, I've seen this question answered very neatly before, so with a bit of digging and some copy/paste I came up with: CREATE TABLE test (test1 VARCHAR(5), test2 varchar(5), test3 varchar(5)) INSERT INTO test(test1, test2, test3) SELECT 'A', '1', 'C' UNION ALL SELECT 'A', '2', 'C' UNION ALL SELECT 'A', '3', 'C' UNION ALL SELECT 'B', '8', 'C' UNION ALL SELECT 'B', '9', 'C' SELECT test1, SUBSTRING((select ', ' + test2 as [text()] from test t where t.test1 = ot.test1 for xml path(''), elements), 3, 100) as test2, test3 FROM test ot GROUP BY test1, test3 DROP TABLE test which seems to work ![]() Good luck! J |
. The only problem is that it
.
#4
| |||
| |||
|
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Hi Bart, What spec server are you using? I can run either script in under a second :-/ J |
. Thank you very much!
#7
| |||
| |||
|
|
I have also tried out the solution below |
#8
| |||
| |||
|
|
I guess there is no function to do this... What is the simplest solution? |
#9
| |||
| |||
|
|
On Fri, 23 Nov 2007 05:52:13 -0800 (PST), Bart op de grote markt wrote: I have also tried out the solution below Hi Bart, Don't use this one - the "trick" it uses is undocumented, so it might stop to work in a future release or even after applying a hotfix. In fact, there have already been situations documented where it doesn't work as expected (I unfortunately lost the URL though). Use the FOR XML trick that J posted instead. However, because of his use of SUBSTRING, the maximum length for the concatenation is limited to 100 characters (or whatever fixed value you use). You can solve that by using STUFF instead of SUBSTRING: SELECT test1, STUFF((SELECT ', ' + test2 AS [text()] FROM test AS t WHERE t.test1 = ot.test1 AND t.test3 = ot.test3 FOR XML PATH(''), ELEMENTS), 1, 2, '') AS test2, test3 FROM test AS ot GROUP BY test1, test3; -- Hugo Kornelis, SQL Server MVP My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis |
.
#10
| |||
| |||
|
|
I guess there is no function to do this... What is the simplest solution? Do it in the front end instead violating 1NF in the Database side. |
![]() |
| Thread Tools | |
| Display Modes | |
| |