如何使用 SQL Server 将行连接成一个单元格并在每个单元格后添加换行符

How to concatenate rows into one cell and add break line after each one using SQL Server

我正在使用 SQL Server 2017,我正在尝试创建一个查询,将每位员工的语言和熟练程度级别串联在一行中。在我的 SQL 数据库中存储信息的 table 例如:

我想达到的最终结果是:

使用 Stuff 函数和 xml 路径,我设法创建了一个 select 查询来显示:

但是我找不到插入分隔线的方法。该查询将用作 AspxGridview 的数据源。

有什么帮助吗?

提前致谢!

我目前的查询:

select distinct
p.PersonID,
STUFF
( (SELECT char(10) + l.Language+' ('+ (case  cvnl.Proficiency when  1 then 'Good'
                                                                    when 2 then 'Very Good'
                                                                    when 3 then 'Excellent'
                                                                    end )
                                                    +') ' FROM CV_NewLanguages cvnl
                                                            inner join Languages l on l.LanguageID = cvnl.LanguageID
    WHERE cvnl.PersonID = p.PersonID

    ORDER BY l.Language ASC FOR XML PATH('')), 1, 1, '') AS Languages
from CV_Certifications cv
       inner join person p on cv.PersonID=p.PersonID
             inner join CV_NewLanguages cvnl on cvnl.PersonID=p.PersonID
               inner join Languages l on l.LanguageID=cvnl.LanguageID
where  active=1      
group by 
p.PersonID,
cvnl.Proficiency,
l.Language
order by p.PersonID

结果是这样的: enter image description here

您可以尝试 Pinal 的以下解决方案。

https://blog.sqlauthority.com/2009/07/01/sql-server-difference-between-line-feed-n-and-carriage-return-r-t-sql-new-line-char/

我在将它的输出绑定到 crystal 报告时使用了它。还没有尝试过使用 aspx gridview。

当您使用 SQL 服务器 2017 时,您可以使用 STRING_AGG 功能

SELECT 
    p.PersonID, 
    STRING_AGG( Language + '(' + 
        CASE cvnl.Proficiency 
            WHEN 1 THEN 'Good'
            WHEN 2 THEN 'Very Good'
            THEN 3 THEN 'Excellent'
        END + ')'
    , CHAR(13) + CHAR(10)) AS Languages
FROM CV_Certifications cv
JOIN person p on cv.PersonID = p.PersonID
JOIN CV_NewLanguages cvnl on cvnl.PersonID = p.PersonID
JOIN Languages l on l.LanguageID = cvnl.LanguageID
WHERE active=1      
GROUP BY p.PersonID,
ORDER BY p.PersonID

您提到 AspxGridview 需要它,因此您可能也需要 HTML 中断

SELECT 
    p.PersonID, 
    STRING_AGG( Language + '(' + 
        CASE cvnl.Proficiency 
            WHEN 1 THEN 'Good'
            WHEN 2 THEN 'Very Good'
            THEN 3 THEN 'Excellent'
        END + ')'
    , CHAR(13) + CHAR(10) + '<BR/>' + CHAR(13) + CHAR(10)) AS Languages
FROM CV_Certifications cv
JOIN person p on cv.PersonID = p.PersonID
JOIN CV_NewLanguages cvnl on cvnl.PersonID = p.PersonID
JOIN Languages l on l.LanguageID = cvnl.LanguageID
WHERE active=1      
GROUP BY p.PersonID,
ORDER BY p.PersonID

如果您使用的是 SQL 以前的版本或者您想继续使用 STUFF,您可以替换 STUFF 结果中的逗号

SELECT 
    p.PersonID,
    REPLACE(
        STUFF( (
            SELECT ',' + l.Language +' (' + 
            CASE cvnl.Proficiency 
                WHEN 1 THEN 'Good'
                WHEN 2 THEN 'Very Good'
                WHEN 3 THEN 'Excellent'
            END +') ' 
            FROM CV_NewLanguages cvnl
            JOIN Languages l on l.LanguageID = cvnl.LanguageID
            WHERE cvnl.PersonID = p.PersonID
            ORDER BY l.Language ASC 
            FOR XML PATH(''))
         , 1, 1, '')
    ,',',CHAR(13) + CHAR(10)) AS Languages
FROM person p  
WHERE EXISTS (SELECT 1 FROM CV_Certifications cv WHERE cv.PersonID = p.PersonID)
AND EXISTS (SELECT 1 FROM CV_NewLanguages cvnl WHERE cvnl.PersonID = p.PersonID
AND active=1      
ORDER BY p.PersonID

我还自由地对您的查询进行了一些调整