MSSQL 使用临时表实现字符串合并处理
2009-09-30 13:20
417 查看
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-09-30 15:16:26
if object_id('tb')is not null drop table tb
go
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',null
UNION ALL SELECT 'a',null
UNION ALL SELECT 'b',null
UNION ALL SELECT 'b',null
declare @col1 varchar(10)
declare @col2 int
update tb set @col2=case when @col1=col1 then @col2+1 else 1 end,
@col1=col1,col2=@col2
--每组 <=2 条的合并
select col1,
col2=ltrim(min(col2))+case when count(*)=1 then ''
else ','+ltrim(max(col2)) end
from tb group by col1
/*
col1 col2
---------- -------------------------
a 1,2
b 1,2
*/
--每组 <=3 条的合并
insert tb select 'b',3
select col1,
col2=CAST(MIN(col2) as varchar)
+CASE
WHEN COUNT(*)=3 THEN ','
+CAST((SELECT col2 FROM tb WHERE col1=a.col1 AND col2 NOT IN(MAX(a.col2),MIN(a.col2))) as varchar)
ELSE ''
END
+CASE
WHEN COUNT(*)>=2 THEN ','+CAST(MAX(col2) as varchar)
ELSE ''
END
from tb a group by col1
/*
col1 col2
---------- ---------------------
a 1,2
b 1,2,3
*/
if object_id('tempdb..#')is not null drop table #
go
select col1,
ltrim(col2)col2
into # from tb order by col1,col2
declare @col1 varchar(10)
declare @col2 varchar(20)
update # set @col2=case when @col1=col1 then @col2+','+col2 else col2 end,
@col1=col1,
col2=@col2
go
select col1,
max(col2)col2
from # group by col1
/*
col1 col2
---------- ------------
a 1,2
b 1,2,3
*/
-->Author:wufeng4552
-->Date :2009-09-30 15:16:26
if object_id('tb')is not null drop table tb
go
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',null
UNION ALL SELECT 'a',null
UNION ALL SELECT 'b',null
UNION ALL SELECT 'b',null
declare @col1 varchar(10)
declare @col2 int
update tb set @col2=case when @col1=col1 then @col2+1 else 1 end,
@col1=col1,col2=@col2
--每组 <=2 条的合并
select col1,
col2=ltrim(min(col2))+case when count(*)=1 then ''
else ','+ltrim(max(col2)) end
from tb group by col1
/*
col1 col2
---------- -------------------------
a 1,2
b 1,2
*/
--每组 <=3 条的合并
insert tb select 'b',3
select col1,
col2=CAST(MIN(col2) as varchar)
+CASE
WHEN COUNT(*)=3 THEN ','
+CAST((SELECT col2 FROM tb WHERE col1=a.col1 AND col2 NOT IN(MAX(a.col2),MIN(a.col2))) as varchar)
ELSE ''
END
+CASE
WHEN COUNT(*)>=2 THEN ','+CAST(MAX(col2) as varchar)
ELSE ''
END
from tb a group by col1
/*
col1 col2
---------- ---------------------
a 1,2
b 1,2,3
*/
if object_id('tempdb..#')is not null drop table #
go
select col1,
ltrim(col2)col2
into # from tb order by col1,col2
declare @col1 varchar(10)
declare @col2 varchar(20)
update # set @col2=case when @col1=col1 then @col2+','+col2 else col2 end,
@col1=col1,
col2=@col2
go
select col1,
max(col2)col2
from # group by col1
/*
col1 col2
---------- ------------
a 1,2
b 1,2,3
*/
相关文章推荐
- 使用临时表合并字符串
- 使用自定义聚集函数来实现分组合并字符串
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- (转载)使用自定义聚集函数来实现分组合并字符串
- 公文处理方案实现之使用模板新建文档并合并正文内容
- MSSQL-字符串分离与列记录合并成一行混合使用
- 如何用java实现不同编码方式字符串的转换(包含异常处理、重复不终止输入、缓冲区bufferedreader的使用)
- 字符串处理是许多程序中非常重要的一部分,它们可以用于文本显示,数据表示,查找键和很多目的.在Unix下,用户可以使用正则表达式的强健功能实现这些 目的,从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础
- 使用ScheduledThreadPoolExecutor实现请求合并处理
- Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
- 不使用库函数实现字符串处理函数
- 自己写的使用聚集函数实现多行字串合并处理
- 使用css方法,实现文字字符串截断处理
- 字符串合并并处理(C++实现)
- C语言:使用链表实现的可变长度字符串处理函数
- 不使用字符串处理的库函数,实现strcpy
- 使用Sharepreference+Thread+Handler进行异步处理实现倒计时功能
- 原串翻转 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)。 给定一个string iniString,请返回一个string,为翻转后的字符串
- 字符串时间实现使用Date类型的函数思路
- 巧用boost库实现字符串映射消息处理函数