如何在 SQL Server 2005 中以逗号分隔的单行显示多行值? [英] How to show multiple row values in comma separated in a single row in SQL Server 2005?

查看:23
本文介绍了如何在 SQL Server 2005 中以逗号分隔的单行显示多行值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我展示了两个表格和结果表格.

Below I have shown two tables and also the result table.

如何以上面显示的方式获取结果表?

How can I get the result table in this manner as I shown on above?

推荐答案

select min(ID) as ID,
       Val,
       stuff((select ','+Cat
              from Table2 as T2
              where T1.Val = T2.Val
              for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '') as Cat
from Table2 as T1
group by Val
order by ID

SQL 小提琴

这篇关于如何在 SQL Server 2005 中以逗号分隔的单行显示多行值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆