本篇内容主要讲解“sqlserver怎么合并列数据”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“sqlserver怎么合并列数据”吧!
sql server 递归查询树型结构某节点的所有上级节点,并且把这些所有上级节点多行拼接为一行,即合并列数据
with eps_root(pk_eps, pk_parent, eps_code, eps_name) as ( SELECT pk_eps, pk_parent, eps_code, eps_name FROM pm_eps where enablestate = 2 and pk_eps = '1001A11000000003P62E' union all SELECT e.pk_eps, e.pk_parent, e.eps_code, e.eps_name FROM pm_eps e inner join eps_root r on e.pk_eps = r.pk_parent where e.enablestate = 2 ) select distinct eps_code = stuff((select '/' + convert(nvarchar(500), eps_code) from (select distinct eps_code from eps_root) r1 for xml path('')), 1, 1, ''), --此次需要使用eps_code排序,不然中文的会错乱顺序 eps_name = stuff((select '/' + eps_name from (select top 99.99 PERCENT eps_name from eps_root order by eps_code) r2 for xml path('')), 1, 1, '') from eps_root
执行sql,如下图效果:
到此,相信大家对“sqlserver怎么合并列数据”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。