Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can I use expressions in Sql Server Reporting services to combine all of the values of a column within a group? I'm trying to accomplish what MySQL's group_concat function does, but in the report (not in the query).

Example. I want to make this data:

Group 1  Value
Test
         A
         B
Test 2
         C
         D

Look this this in the report:

Group 1 Value
test    A, B
test 2  C, D
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
822 views
Welcome To Ask or Share your Answers For Others

1 Answer

try something like this (works on SQL Server 2005 and up):

set nocount on;
declare @t table (id int, name varchar(20), x char(1))
insert into @t (id, name, x)
select 1,'test1', 'a' union
select 1,'test1', 'b' union
select 1,'test1', 'c' union
select 2,'test2', 'a' union
select 2,'test2', 'c' union
select 3,'test3', 'b' union
select 3,'test3', 'c' 
SET NOCOUNT OFF

SELECT p1.id, p1.name,
          stuff(
                   (SELECT
                        ', ' + x
                        FROM @t p2
                        WHERE p2.id=p1.id
                        ORDER BY name, x
                        FOR XML PATH('') 
                   )
                   ,1,2, ''
               ) AS p3
      FROM @t p1
     GROUP BY 
        id, name

OUTPUT:

id          name                 p3
----------- -------------------- ---------
1           test1                a, b, c
2           test2                a, c
3           test3                b, c

(3 row(s) affected)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...