aa 07 q
aa 08 w
aa 09 e
bb 07 r
bb 08 t
bb 09 y
合并之后变成
name<varchar> peopom<varchar>
aa 07q08w09e
bb 07r08t09y
20 个解决方案
#1
--各种字符串分函数
--3.3.1 使用游标法进行字符串合并处理的示例。
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
--合并处理
--定义结果集表变量
DECLARE @t TABLE(col1 varchar(10),col2 varchar(100))
--定义游标并进行合并处理
DECLARE tb CURSOR LOCAL
FOR
SELECT col1,col2 FROM tb ORDER BY col1,col2
DECLARE @col1_old varchar(10),@col1 varchar(10),@col2 int,@s varchar(100)
OPEN tb
FETCH tb INTO @col1,@col2
SELECT @col1_old=@col1,@s=''
WHILE @@FETCH_STATUS=0
BEGIN
IF @col1=@col1_old
SELECT @s=@s+','+CAST(@col2 as varchar)
ELSE
BEGIN
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
SELECT @s=','+CAST(@col2 as varchar),@col1_old=@col1
END
FETCH tb INTO @col1,@col2
END
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
CLOSE tb
DEALLOCATE tb
--显示结果并删除测试数据
SELECT * FROM @t
DROP TABLE tb
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
GO
/*==============================================*/
--3.3.2 使用用户定义函数,配合SELECT处理完成字符串合并处理的示例
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
GO
--合并处理函数
CREATE FUNCTION dbo.f_str(@col1 varchar(10))
RETURNS varchar(100)
AS
BEGIN
DECLARE @re varchar(100)
SET @re=''
SELECT @re=@re+','+CAST(col2 as varchar)
FROM tb
WHERE col1=@col1
RETURN(STUFF(@re,1,1,''))
END
GO
--调用函数
SELECT col1,col2=dbo.f_str(col1) FROM tb GROUP BY col1
--删除测试
DROP TABLE tb
DROP FUNCTION f_str
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
GO
/*==============================================*/
--3.3.3 使用临时表实现字符串合并处理的示例
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
--合并处理
SELECT col1,col2=CAST(col2 as varchar(100))
INTO #t FROM tb
ORDER BY col1,col2
DECLARE @col1 varchar(10),@col2 varchar(100)
UPDATE #t SET
@col2=CASE WHEN @col1=col1 THEN @col2+','+col2 ELSE col2 END,
@col1=col1,
col2=@col2
SELECT * FROM #t
/*--更新处理后的临时表
col1 col2
---------- -------------
a 1
a 1,2
b 1
b 1,2
b 1,2,3
--*/
--得到最终结果
SELECT col1,col2=MAX(col2) FROM #t GROUP BY col1
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
--删除测试
DROP TABLE tb,#t
GO
/*==============================================*/
--3.3.4.1 每组 <=2 条记录的合并
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'c',3
--合并处理
SELECT col1,
col2=CAST(MIN(col2) as varchar)
+CASE
WHEN COUNT(*)=1 THEN ''
ELSE ','+CAST(MAX(col2) as varchar)
END
FROM tb
GROUP BY col1
DROP TABLE tb
/*--结果
col1 col2
---------- ----------
a 1,2
b 1,2
c 3
--*/
--3.3.4.2 每组 <=3 条记录的合并
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
UNION ALL SELECT 'c',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
DROP TABLE tb
/*--结果
col1 col2
---------- ------------
a 1,2
b 1,2,3
c 3
--*/
GO
DECLARE @t TABLE(id int, value varchar(10))
INSERT @t SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM @t)A
OUTER APPLY(SELECT
[values]= STUFF(REPLACE(REPLACE((
SELECT value FROM @t N WHERE id = A.id
FOR XML AUTO),'<N value="',','),'"/>', ''), 1, 1, ''))N
--结果
----------- ----------------
1 aa,bb
2 aaa,bbb,ccc
#2
2005的
declare @t table (k int identity, a char);
insert into @t values ('S');
insert into @t values ('S');
insert into @t values ('B');
insert into @t values (' ');
insert into @t values ('R');
insert into @t values ('o');
insert into @t values ('c');
insert into @t values ('k');
insert into @t values ('s');
insert into @t values ('!');
with cte(a, k)
as
(
select cast(t.a as varchar(max)), t.k from @t t
where k = 1
union all
select c.a + cast(t.a as varchar(max)), t.k
from @t t
join cte c on t.k = c.k+1
)
select top 1 a from cte order by k desc
xml
declare @t table (k int identity, a char);
insert into @t values ('S');
insert into @t values ('S');
insert into @t values ('B');
insert into @t values (' ');
insert into @t values ('R');
insert into @t values ('o');
insert into @t values ('c');
insert into @t values ('k');
insert into @t values ('s');
insert into @t values ('!');
SELECT
CONVERT(VARCHAR(50), myData) AS MyString
FROM
(
SELECT a AS [text()]
FROM @t
ORDER BY k
FOR XML PATH(''), TYPE
) x (myData)
create table tb1(编码 char(4),姓名 varchar(20))
insert tb1 select '0001' , '小李'
insert tb1 select '0002' , '小陈'
insert tb1 select '0003' , '小张'
create table tb2(编码 char(4),部门 varchar(20))
insert tb2 select '0001','信息部'
insert tb2 select '0001','操作部'
insert tb2 select '0002','办公室'
insert tb2 select '0003','仓库部'
insert tb2 select '0003','办公室'
SELECT B.编码,c.姓名,b.部门 FROM
(SELECT * FROM (select distinct 编码 from tb2) a OUTER apply
(SELECT
stuff(CONVERT(VARCHAR(50), mydata),1,1,'') AS 部门
FROM
(
select '、'+部门 as [text()]from tb2 where 编码=a.编码 for xml path(''),type
) x (myData)
)N)B inner join tb1 c on b.编码=c.编码
#3
http://topic.csdn.net/u/20080123/18/9731d130-0d4b-4c11-8d89-f2c3ca331f0c.html
#4
create function f_str(@name varchar(100))
returns varchar(100)
as
begin
declare @s varchar(200)
select @s = Isnull(@s,'')+peo+pom from ta where name = @name
return @s
end
go
select name,dbo.f_str(name)
from ta
group by name
#5
create table hb(name varchar(2),peo varchar(02),pom varchar(1))
insert into hb values('aa','07','q')
insert into hb values('aa','08','w')
insert into hb values('aa','09','e')
insert into hb values('bb','07','r')
insert into hb values('bb','08','t')
insert into hb values('bb','09','y')
create FUNCTION dbo.f_str(@col1 varchar(10))
RETURNS varchar(100)
AS
BEGIN
DECLARE @re varchar(100)
SET @re=''
SELECT @re=@re+peo+pom from hb where name=@col1
return @re
END
select name,dbo.f_str(name) from hb group by name
name
---- ----------------------------------------------------------------------------------------------------
aa 07q08w09e
bb 07r08t09y
(2 行受影响)
#6
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (name varchar(2),peo varchar(2),pom varchar(1))
insert into #T
select 'aa','07','q' union all
select 'aa','08','w' union all
select 'aa','09','e' union all
select 'bb','07','r' union all
select 'bb','08','t' union all
select 'bb','09','y'
-->(1) 2000: 不用函数的解决方案
declare @i int, @max int, @sql varchar(8000)
select top 1 @i=1, @max=count(*) from #T group by name order by 2 desc
while @i<=@max
begin
set @sql=isnull(@sql+'+','')+'max(case i when '+ltrim(@i)+' then peo+pom else '''' end)'
set @i=@i+1
end
exec ('select name, com='+@sql+' from (select *,i=(select count(1) from #T where name=a.name and peo<=a.peo) from #T a) t group by name')
/*
name com
---- ---------
aa 07q08w09e
bb 07r08t09y
*/
-->(2) 2005:
select name, com=(select peo+pom from #T where name=a.name for xml path('')) from #T a group by name
/*
name com
---- ---------
aa 07q08w09e
bb 07r08t09y
*/
#7
合并列值
原著:邹建
改编:爱新觉罗.毓华 2007-12-16 广东深圳
表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value FROM tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id
drop table tb
drop function dbo.f_str
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
--2、另外一种函数.
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
--创建一个合并的函数
create function f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb
drop table tb
drop function dbo.f_hb
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb
/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(2 行受影响)
*/
--SQL2005中的方法2(csdn dobear_0922 提供)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id
/*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc
(2 row(s) affected)
*/
drop table tb
原著:邹建
改编:爱新觉罗.毓华 2007-12-16 广东深圳
表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value FROM tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id
drop table tb
drop function dbo.f_str
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
--2、另外一种函数.
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
--创建一个合并的函数
create function f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb
drop table tb
drop function dbo.f_hb
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb
/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(2 行受影响)
*/
--SQL2005中的方法2(csdn dobear_0922 提供)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id
/*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc
(2 row(s) affected)
*/
drop table tb
#8
--以下为2000中利用函数的办法,2005的办法见7楼.
create table tb(name varchar(10) , peo varchar(10) , pom varchar(10) )
insert into tb values('aa' , '07' , 'q')
insert into tb values('aa' , '08' , 'w')
insert into tb values('aa' , '09' , 'e')
insert into tb values('bb' , '07' , 'r')
insert into tb values('bb' , '08' , 't')
insert into tb values('bb' , '09' , 'y')
go
--创建一个合并的函数
create function f_hb(@name varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + cast(peo as varchar) + cast(pom as varchar) from tb where name = @name
--set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct name ,dbo.f_hb(name) as peopom from tb
drop table tb
drop function dbo.f_hb
/*
name peopom
---------- ----------
aa 07q08w09e
bb 07r08t09y
(所影响的行数为 2 行)
*/
#9
各位,高手能不能根据我上面的要求来实现,请看清楚点,
#10
6楼
#11
8楼
#12
见笑了 笨方法
CREATE TABLE AA
(
A VARCHAR (20),
B VARCHAR (20),
C VARCHAR (20)
)
INSERT INTO AA VALUES ('AA','BB','CC')
INSERT INTO AA VALUES ('DD','FF','GG')
CREATE TABLE #C
(
A VARCHAR (20),
BC VARCHAR (20),
)
insert INTO #C SELECT A ,B+C FROM AA
SELECT * FROM #C
#13
6楼和8楼,实现了,谢谢了
#14
前面第一个对的给多了10分
#15
不错。。。顶一下
#16
留个记号
#17
留个记号
#18
谢谢,一直在寻找
#19
嘿嘿 终于解决了 7楼的先生谢谢你了哦
#20
虽然解决了,但是方法太复杂了。
SELECT 字段+',' FROM 表名WHERE 条件 FOR XML PATH('')
这个多简单。
SELECT 字段+',' FROM 表名WHERE 条件 FOR XML PATH('')
这个多简单。
#21
#1
--各种字符串分函数
--3.3.1 使用游标法进行字符串合并处理的示例。
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
--合并处理
--定义结果集表变量
DECLARE @t TABLE(col1 varchar(10),col2 varchar(100))
--定义游标并进行合并处理
DECLARE tb CURSOR LOCAL
FOR
SELECT col1,col2 FROM tb ORDER BY col1,col2
DECLARE @col1_old varchar(10),@col1 varchar(10),@col2 int,@s varchar(100)
OPEN tb
FETCH tb INTO @col1,@col2
SELECT @col1_old=@col1,@s=''
WHILE @@FETCH_STATUS=0
BEGIN
IF @col1=@col1_old
SELECT @s=@s+','+CAST(@col2 as varchar)
ELSE
BEGIN
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
SELECT @s=','+CAST(@col2 as varchar),@col1_old=@col1
END
FETCH tb INTO @col1,@col2
END
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
CLOSE tb
DEALLOCATE tb
--显示结果并删除测试数据
SELECT * FROM @t
DROP TABLE tb
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
GO
/*==============================================*/
--3.3.2 使用用户定义函数,配合SELECT处理完成字符串合并处理的示例
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
GO
--合并处理函数
CREATE FUNCTION dbo.f_str(@col1 varchar(10))
RETURNS varchar(100)
AS
BEGIN
DECLARE @re varchar(100)
SET @re=''
SELECT @re=@re+','+CAST(col2 as varchar)
FROM tb
WHERE col1=@col1
RETURN(STUFF(@re,1,1,''))
END
GO
--调用函数
SELECT col1,col2=dbo.f_str(col1) FROM tb GROUP BY col1
--删除测试
DROP TABLE tb
DROP FUNCTION f_str
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
GO
/*==============================================*/
--3.3.3 使用临时表实现字符串合并处理的示例
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
--合并处理
SELECT col1,col2=CAST(col2 as varchar(100))
INTO #t FROM tb
ORDER BY col1,col2
DECLARE @col1 varchar(10),@col2 varchar(100)
UPDATE #t SET
@col2=CASE WHEN @col1=col1 THEN @col2+','+col2 ELSE col2 END,
@col1=col1,
col2=@col2
SELECT * FROM #t
/*--更新处理后的临时表
col1 col2
---------- -------------
a 1
a 1,2
b 1
b 1,2
b 1,2,3
--*/
--得到最终结果
SELECT col1,col2=MAX(col2) FROM #t GROUP BY col1
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
--删除测试
DROP TABLE tb,#t
GO
/*==============================================*/
--3.3.4.1 每组 <=2 条记录的合并
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'c',3
--合并处理
SELECT col1,
col2=CAST(MIN(col2) as varchar)
+CASE
WHEN COUNT(*)=1 THEN ''
ELSE ','+CAST(MAX(col2) as varchar)
END
FROM tb
GROUP BY col1
DROP TABLE tb
/*--结果
col1 col2
---------- ----------
a 1,2
b 1,2
c 3
--*/
--3.3.4.2 每组 <=3 条记录的合并
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
UNION ALL SELECT 'c',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
DROP TABLE tb
/*--结果
col1 col2
---------- ------------
a 1,2
b 1,2,3
c 3
--*/
GO
DECLARE @t TABLE(id int, value varchar(10))
INSERT @t SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM @t)A
OUTER APPLY(SELECT
[values]= STUFF(REPLACE(REPLACE((
SELECT value FROM @t N WHERE id = A.id
FOR XML AUTO),'<N value="',','),'"/>', ''), 1, 1, ''))N
--结果
----------- ----------------
1 aa,bb
2 aaa,bbb,ccc
#2
2005的
declare @t table (k int identity, a char);
insert into @t values ('S');
insert into @t values ('S');
insert into @t values ('B');
insert into @t values (' ');
insert into @t values ('R');
insert into @t values ('o');
insert into @t values ('c');
insert into @t values ('k');
insert into @t values ('s');
insert into @t values ('!');
with cte(a, k)
as
(
select cast(t.a as varchar(max)), t.k from @t t
where k = 1
union all
select c.a + cast(t.a as varchar(max)), t.k
from @t t
join cte c on t.k = c.k+1
)
select top 1 a from cte order by k desc
xml
declare @t table (k int identity, a char);
insert into @t values ('S');
insert into @t values ('S');
insert into @t values ('B');
insert into @t values (' ');
insert into @t values ('R');
insert into @t values ('o');
insert into @t values ('c');
insert into @t values ('k');
insert into @t values ('s');
insert into @t values ('!');
SELECT
CONVERT(VARCHAR(50), myData) AS MyString
FROM
(
SELECT a AS [text()]
FROM @t
ORDER BY k
FOR XML PATH(''), TYPE
) x (myData)
create table tb1(编码 char(4),姓名 varchar(20))
insert tb1 select '0001' , '小李'
insert tb1 select '0002' , '小陈'
insert tb1 select '0003' , '小张'
create table tb2(编码 char(4),部门 varchar(20))
insert tb2 select '0001','信息部'
insert tb2 select '0001','操作部'
insert tb2 select '0002','办公室'
insert tb2 select '0003','仓库部'
insert tb2 select '0003','办公室'
SELECT B.编码,c.姓名,b.部门 FROM
(SELECT * FROM (select distinct 编码 from tb2) a OUTER apply
(SELECT
stuff(CONVERT(VARCHAR(50), mydata),1,1,'') AS 部门
FROM
(
select '、'+部门 as [text()]from tb2 where 编码=a.编码 for xml path(''),type
) x (myData)
)N)B inner join tb1 c on b.编码=c.编码
#3
http://topic.csdn.net/u/20080123/18/9731d130-0d4b-4c11-8d89-f2c3ca331f0c.html
#4
create function f_str(@name varchar(100))
returns varchar(100)
as
begin
declare @s varchar(200)
select @s = Isnull(@s,'')+peo+pom from ta where name = @name
return @s
end
go
select name,dbo.f_str(name)
from ta
group by name
#5
create table hb(name varchar(2),peo varchar(02),pom varchar(1))
insert into hb values('aa','07','q')
insert into hb values('aa','08','w')
insert into hb values('aa','09','e')
insert into hb values('bb','07','r')
insert into hb values('bb','08','t')
insert into hb values('bb','09','y')
create FUNCTION dbo.f_str(@col1 varchar(10))
RETURNS varchar(100)
AS
BEGIN
DECLARE @re varchar(100)
SET @re=''
SELECT @re=@re+peo+pom from hb where name=@col1
return @re
END
select name,dbo.f_str(name) from hb group by name
name
---- ----------------------------------------------------------------------------------------------------
aa 07q08w09e
bb 07r08t09y
(2 行受影响)
#6
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (name varchar(2),peo varchar(2),pom varchar(1))
insert into #T
select 'aa','07','q' union all
select 'aa','08','w' union all
select 'aa','09','e' union all
select 'bb','07','r' union all
select 'bb','08','t' union all
select 'bb','09','y'
-->(1) 2000: 不用函数的解决方案
declare @i int, @max int, @sql varchar(8000)
select top 1 @i=1, @max=count(*) from #T group by name order by 2 desc
while @i<=@max
begin
set @sql=isnull(@sql+'+','')+'max(case i when '+ltrim(@i)+' then peo+pom else '''' end)'
set @i=@i+1
end
exec ('select name, com='+@sql+' from (select *,i=(select count(1) from #T where name=a.name and peo<=a.peo) from #T a) t group by name')
/*
name com
---- ---------
aa 07q08w09e
bb 07r08t09y
*/
-->(2) 2005:
select name, com=(select peo+pom from #T where name=a.name for xml path('')) from #T a group by name
/*
name com
---- ---------
aa 07q08w09e
bb 07r08t09y
*/
#7
合并列值
原著:邹建
改编:爱新觉罗.毓华 2007-12-16 广东深圳
表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value FROM tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id
drop table tb
drop function dbo.f_str
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
--2、另外一种函数.
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
--创建一个合并的函数
create function f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb
drop table tb
drop function dbo.f_hb
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb
/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(2 行受影响)
*/
--SQL2005中的方法2(csdn dobear_0922 提供)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id
/*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc
(2 row(s) affected)
*/
drop table tb
原著:邹建
改编:爱新觉罗.毓华 2007-12-16 广东深圳
表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value FROM tb WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id
drop table tb
drop function dbo.f_str
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
--2、另外一种函数.
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
--创建一个合并的函数
create function f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb
drop table tb
drop function dbo.f_hb
/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/
2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb
/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(2 行受影响)
*/
--SQL2005中的方法2(csdn dobear_0922 提供)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id
/*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc
(2 row(s) affected)
*/
drop table tb
#8
--以下为2000中利用函数的办法,2005的办法见7楼.
create table tb(name varchar(10) , peo varchar(10) , pom varchar(10) )
insert into tb values('aa' , '07' , 'q')
insert into tb values('aa' , '08' , 'w')
insert into tb values('aa' , '09' , 'e')
insert into tb values('bb' , '07' , 'r')
insert into tb values('bb' , '08' , 't')
insert into tb values('bb' , '09' , 'y')
go
--创建一个合并的函数
create function f_hb(@name varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + cast(peo as varchar) + cast(pom as varchar) from tb where name = @name
--set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct name ,dbo.f_hb(name) as peopom from tb
drop table tb
drop function dbo.f_hb
/*
name peopom
---------- ----------
aa 07q08w09e
bb 07r08t09y
(所影响的行数为 2 行)
*/
#9
各位,高手能不能根据我上面的要求来实现,请看清楚点,
#10
6楼
#11
8楼
#12
见笑了 笨方法
CREATE TABLE AA
(
A VARCHAR (20),
B VARCHAR (20),
C VARCHAR (20)
)
INSERT INTO AA VALUES ('AA','BB','CC')
INSERT INTO AA VALUES ('DD','FF','GG')
CREATE TABLE #C
(
A VARCHAR (20),
BC VARCHAR (20),
)
insert INTO #C SELECT A ,B+C FROM AA
SELECT * FROM #C
#13
6楼和8楼,实现了,谢谢了
#14
前面第一个对的给多了10分
#15
不错。。。顶一下
#16
留个记号
#17
留个记号
#18
谢谢,一直在寻找
#19
嘿嘿 终于解决了 7楼的先生谢谢你了哦
#20
虽然解决了,但是方法太复杂了。
SELECT 字段+',' FROM 表名WHERE 条件 FOR XML PATH('')
这个多简单。
SELECT 字段+',' FROM 表名WHERE 条件 FOR XML PATH('')
这个多简单。