How to concatenate the text when group by a partic

2019-09-17 03:58发布

This question already has an answer here:

Hi I have a table StudentNote with three fields 'StudentID','Notes','Date'.i have the following values

enter image description here

Now my requirement is ,i wanted to group the above table on StudentID and Concatenate Note and Date Fields like

enter image description here

How can i write a sql to achieve this.

Thanks in advance

1条回答
男人必须洒脱
2楼-- · 2019-09-17 04:31

try this

SELECT T1.STUDENTID,
       STUFF((SELECT ',' + CONVERT(VARCHAR(50), NOTE) + ','
                     + CONVERT(VARCHAR(50), DATE)
              FROM   STUDENTNOTE B
              WHERE  B.STUDENTID = T1.STUDENTID
              FOR XML PATH('')), 1, 1, '') [NOTE]
FROM   STUDENTNOTE T1
GROUP  BY T1.STUDENTID 
查看更多
登录 后发表回答