34,838
社区成员




create table cust
(
custno varchar(6),
po int,
color int
)
go
insert into cust
select '09864R', 7075, 126 union all
select '09864R', 9387, 126 union all
select '09864R', 8971, 126 union all
select '09864R', 9387, 291 union all
select '09864R', 7075, 291 union all
select '09864R', 8971, 291 union all
select '09864Q', 8812, 281 union all
select '09864Q', 8813, 281 union all
select '09864Q', 8971, 222 union all
select '09864Q', 7075, 222 union all
select '09864Q', 9387, 222
go
alter function dbo.mytt
(
@custno varchar(6)
)
returns varchar(3000)
as
begin
declare @str varchar(3000)
set @str='';
select @str=@str+'-'+cast(po as varchar(4)) from cust where custno=@custno
set @str=substring(@str,2,3000);
return @str
end
go
select custno,dbo.mytt(custno)as t,color from cust group by custno,color
create table tb (custno varchar(255), po int , color int)
insert into tb
select '09864R', 7075,126 union all
select '09864R', 9387,126 union all
select '09864R', 8971,126 union all
select '09864R', 9387,291 union all
select '09864R', 7075,291 union all
select '09864R', 8971,291 union all
select '09864Q', 8812,281 union all
select '09864Q', 8813,281 union all
select '09864Q', 8971,222 union all
select '09864Q', 7075,222 union all
select '09864Q', 9387,222
create function fn_test(
@a varchar(255),@b int
)
returns varchar(200)
as
begin
declare @str varchar(20)
select @str = isnull(@str+'-','')+convert(varchar,po) from tb where custno= @a and color= @b
return @str
end
go
select custno,dbo.fn_test(custno,color),color from tb group by custno,color
---2000参考
/******************************************************************************************************************************************************
合并分拆表数据
整理人:中国风(Roy)
日期:2008.06.06
******************************************************************************************************************************************************/
--> --> (Roy)生成測試數據
if not object_id('Tab') is null
drop table Tab
Go
Create table Tab([Col1] int,[Col2] nvarchar(1))
Insert Tab
select 1,N'a' union all
select 1,N'b' union all
select 1,N'c' union all
select 2,N'd' union all
select 2,N'e' union all
select 3,N'f'
Go
合并表:
SQL2000用函数:
go
if object_id('F_Str') is not null
drop function F_Str
go
create function F_Str(@Col1 int)
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1
return @S
end
go
Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab
go
SQL2005用XML:
方法1:
select
a.Col1,Col2=stuff(b.Col2.value('/R[1]','nvarchar(max)'),1,1,'')
from
(select distinct COl1 from Tab) a
Cross apply
(select COl2=(select N','+Col2 from Tab where Col1=a.COl1 For XML PATH(''), ROOT('R'), TYPE))b
方法2:
select
a.Col1,COl2=replace(b.Col2.value('/Tab[1]','nvarchar(max)'),char(44)+char(32),char(44))
from
(select distinct COl1 from Tab) a
cross apply
(select Col2=(select COl2 from Tab where COl1=a.COl1 FOR XML AUTO, TYPE)
.query(' <Tab>
{for $i in /Tab[position() <last()]/@COl2 return concat(string($i),",")}
{concat("",string(/Tab[last()]/@COl2))}
</Tab>')
)b
SQL05用CTE:
;with roy as(select Col1,Col2,row=row_number()over(partition by COl1 order by COl1) from Tab)
,Roy2 as
(select COl1,cast(COl2 as nvarchar(100))COl2,row from Roy where row=1
union all
select a.Col1,cast(b.COl2+','+a.COl2 as nvarchar(100)),a.row from Roy a join Roy2 b on a.COl1=b.COl1 and a.row=b.row+1)
select Col1,Col2 from Roy2 a where row=(select max(row) from roy where Col1=a.COl1) order by Col1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- ------------
1 a,b,c
2 d,e
3 f
(3 行受影响)
*/
create table cust(custno varchar(10), po int, color int)
insert into cust values('09864R', 7075 ,126 )
insert into cust values('09864R', 9387 ,126 )
insert into cust values('09864R', 8971 ,126 )
insert into cust values('09864R', 9387 ,291 )
insert into cust values('09864R', 7075 ,291 )
insert into cust values('09864R', 8971 ,291 )
insert into cust values('09864Q', 8812 ,281 )
insert into cust values('09864Q', 8813 ,281 )
insert into cust values('09864Q', 8971 ,222 )
insert into cust values('09864Q', 7075 ,222 )
insert into cust values('09864Q', 9387 ,222 )
go
create function dbo.f_str(@custno varchar(10) , @color int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + '-' + cast(po as varchar) from cust where custno = @custno and color = @color
set @str = right(@str , len(@str) - 1)
return @str
end
go
--调用函数
select custno , po = dbo.f_str(custno , color) , color from cust group by custno , color
drop function dbo.f_str
drop table cust
/*
custno po
---------- ---------------
09864Q 8971-7075-9387 222
09864Q 8812-8813 281
09864R 7075-9387-8971 126
09864R 9387-7075-8971 291
(所影响的行数为 4 行)
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-30 16:06:21
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([custno] varchar(6),[po] int,[color] int)
insert [tb]
select '09864R',7075,126 union all
select '09864R',9387,126 union all
select '09864R',8971,126 union all
select '09864R',9387,291 union all
select '09864R',7075,291 union all
select '09864R',8971,291 union all
select '09864Q',8812,281 union all
select '09864Q',8813,281 union all
select '09864Q',8971,222 union all
select '09864Q',7075,222 union all
select '09864Q',9387,222
--------------开始查询--------------------------
select
custno,
[po]=stuff((select '-'+ltrim([po]) from tb where color=t.color for xml path('')), 1, 1, ''),
color
from
[tb] t
group by
custno,color
----------------结果----------------------------
/*custno po color
------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
09864Q 8971-7075-9387 222
09864Q 8812-8813 281
09864R 7075-9387-8971 126
09864R 9387-7075-8971 291
(4 行受影响)
*/
--> --> (Roy)生成測試數據
if not object_id('Tab') is null
drop table Tab
Go
Create table Tab([Col1] int,[Col2] nvarchar(1))
Insert Tab
select 1,N'a' union all
select 1,N'b' union all
select 1,N'c' union all
select 2,N'd' union all
select 2,N'e' union all
select 3,N'f'
Go
合并表:
SQL2000用函数:
go
if object_id('F_Str') is not null
drop function F_Str
go
create function F_Str(@Col1 int)
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1
return @S
end
go
Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab
go
SQL2005用XML:
方法1:
select
a.Col1,Col2=stuff(b.Col2.value('/R[1]','nvarchar(max)'),1,1,'')
from
(select distinct COl1 from Tab) a
Cross apply
(select COl2=(select N','+Col2 from Tab where Col1=a.COl1 For XML PATH(''), ROOT('R'), TYPE))b
方法2:
select
a.Col1,COl2=replace(b.Col2.value('/Tab[1]','nvarchar(max)'),char(44)+char(32),char(44))
from
(select distinct COl1 from Tab) a
cross apply
(select Col2=(select COl2 from Tab where COl1=a.COl1 FOR XML AUTO, TYPE)
.query('<Tab>
{for $i in /Tab[position()<last()]/@COl2 return concat(string($i),",")}
{concat("",string(/Tab[last()]/@COl2))}
</Tab>')
)b
SQL05用CTE:
;with roy as(select Col1,Col2,row=row_number()over(partition by COl1 order by COl1) from Tab)
,Roy2 as
(select COl1,cast(COl2 as nvarchar(100))COl2,row from Roy where row=1
union all
select a.Col1,cast(b.COl2+','+a.COl2 as nvarchar(100)),a.row from Roy a join Roy2 b on a.COl1=b.COl1 and a.row=b.row+1)
select Col1,Col2 from Roy2 a where row=(select max(row) from roy where Col1=a.COl1) order by Col1 option (MAXRECURSION 0)
生成结果:
/*
Col1 COl2
----------- ------------
1 a,b,c
2 d,e
3 f
(3 行受影响)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (custno nvarchar(12),po int,color int)
insert into [tb]
select '09864R',7075,126 union all
select '09864R',9387,126 union all
select '09864R',8971,126 union all
select '09864R',9387,291 union all
select '09864R',7075,291 union all
select '09864R',8971,291 union all
select '09864Q',8812,281 union all
select '09864Q',8813,281 union all
select '09864Q',8971,222 union all
select '09864Q',7075,222 union all
select '09864Q',9387,222
select custno,
po=stuff((select '-'+ltrim(po) from tb where custno=t.custno and color=t.color for xml path('')),1,1,''),
color
from tb t group by color,custno
/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳
描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id value
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
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(100)
as
begin
declare @str varchar(1000)
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 id , value = dbo.f_str(id) from tb group by id
drop function dbo.f_str
drop table tb
--2、sql2005中的方法
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, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id
drop table tb
--3、使用游标合并数据
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
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursor
select * from @t
drop table tb