博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL 多列合并成一列
阅读量:5107 次
发布时间:2019-06-13

本文共 1307 字,大约阅读时间需要 4 分钟。

方法一:
create table #Tmp (nu int, val varchar(100))
insert into #Tmp values(1, 'a')
insert into #Tmp values(2, 'b')
insert into #Tmp values(2, 'c')
insert into #Tmp values(3, 'd')
insert into #Tmp values(3, 'e')
insert into #Tmp values(3, 'f')
select * from #Tmp
;with Orign as
(
select ROW_NUMBER() over(partition by nu order by nu) as rn, nu, val from #Tmp
),
recur as
(
select * from Orign where rn = 1
union all
select a.rn, , cast(b.val + ',' + a.val as varchar(100))as val
from Orign a
inner join recur b
on = and a.rn = b.rn+1
)
select nu, val
from recur a
where a.rn = (select max(rn)from recur b where = )
order by nu
drop table #Tmp
方法二:
create table #Tmp (nu int, val varchar(100))
insert into #Tmp values(1, 'a')
insert into #Tmp values(2, 'b')
insert into #Tmp values(2, 'c')
insert into #Tmp values(3, 'd')
insert into #Tmp values(3, 'e')
insert into #Tmp values(3, 'f')
insert into #Tmp values(4, 'g')
insert into #Tmp values(4, 'h')
insert into #Tmp values(4, 'i')
select * from #Tmp
select ','+val as [text()] from #Tmp t2 for xml path ('')
select nu, stuff( (select ','+val as [text()] from #Tmp t2 where = for xml path ('')),1,1,'')
from #Tmp t1
group by nu
drop table #Tmp
类别:  
posted on
2009-05-24 13:44 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/AlanGan/archive/2009/05/24/1905779.html

你可能感兴趣的文章
Nginx配置文件详细说明
查看>>
遇到的Mysql的一个坑
查看>>
AC日记——「HNOI2017」单旋 LiBreOJ 2018
查看>>
vue总结
查看>>
真机调试的准备工作介绍
查看>>
(笔记)Linux内核学习(十一)之I/O层和I/O调度机制
查看>>
[lintcode medium] Delete digits
查看>>
3.29下午
查看>>
macOS升级到high Sierra后, Cocoapods不能使用解决办法
查看>>
vmstat详细说明
查看>>
php类点滴---访问修饰符public protected private
查看>>
spring-boot的helloWorld详解
查看>>
Codeforces 919 A. Supermarket
查看>>
NYOJ 21.三个水杯-初始态到目标态的最少次数-经典BFS
查看>>
实验四+164+张增进
查看>>
第09次:升级《陋习手记》滑动和对话框
查看>>
url传参(所传的参数为数字,汉字。获取该参数为汉字乱码)
查看>>
简单了解下CGI、FastCGI和php-fpm的概念和区别和运行原理
查看>>
TIME_WAIT 太多的解决办法[转载]
查看>>
低版本中使用高版本出现的类怎么办?
查看>>