表名: Table1
字段: ID F1 F2
记录:1 A X1
1 B X2
1 C X3
2 A Y1
2 B Y2
想通过SQL语句一句话写完求得结果如下:
ID optionpack
1 A.X1;B.X2;C.X3
2 A.Y1;B.Y2
不知道哪位高人给个解法? 先谢了!
6 个解决方案
#1
用函數
--示例
--建表
create table tb (A varchar(10),B varchar(1000))
Insert into tb
select '111','AA0000,AA0001,AA0002,'
union all select '111','BB0000,BB0001,BB0002,'
union all select '222','AA0120,AA0102,AA1453,'
union all select '222','BB1212,BB3423,BB2123,'
select * from tb
--函數
create function dbo.fn_b(@a varchar(10))
returns varchar(1000)
as
begin
declare @s varchar(1000)
set @s=''
select @s=@s+[B] from tb where A=@a
return (@s)
end
--刪除
drop table tb
drop function dbo.fn_b
--結果
select A,B=dbo.fn_b(A) from tb group by A
A B
-------------------------------------------
111 AA0000,AA0001,AA0002,BB0000,BB0001,BB0002,
222 AA0120,AA0102,AA1453,BB1212,BB3423,BB2123,
--示例
--建表
create table tb (A varchar(10),B varchar(1000))
Insert into tb
select '111','AA0000,AA0001,AA0002,'
union all select '111','BB0000,BB0001,BB0002,'
union all select '222','AA0120,AA0102,AA1453,'
union all select '222','BB1212,BB3423,BB2123,'
select * from tb
--函數
create function dbo.fn_b(@a varchar(10))
returns varchar(1000)
as
begin
declare @s varchar(1000)
set @s=''
select @s=@s+[B] from tb where A=@a
return (@s)
end
--刪除
drop table tb
drop function dbo.fn_b
--結果
select A,B=dbo.fn_b(A) from tb group by A
A B
-------------------------------------------
111 AA0000,AA0001,AA0002,BB0000,BB0001,BB0002,
222 AA0120,AA0102,AA1453,BB1212,BB3423,BB2123,
#2
create table ta(ID nvarchar(2),F1 nvarchar(1),F2 nvarchar(3))
insert ta select '1' , 'A' , 'X1' union all select
'1' , 'B' , 'X2' union all select
'1' ,'C' , 'X3' union all select
'2' , 'A' ,'Y1' union all select
'2' ,'B' ,'Y2'
select * from ta
create function f_str(@id varchar(10))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+rtrim(f2)
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
insert ta select '1' , 'A' , 'X1' union all select
'1' , 'B' , 'X2' union all select
'1' ,'C' , 'X3' union all select
'2' , 'A' ,'Y1' union all select
'2' ,'B' ,'Y2'
select * from ta
create function f_str(@id varchar(10))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+rtrim(f2)
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
#3
create function f_str(@id varchar(10))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+'.'+rtrim(f2)+';'
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+'.'+rtrim(f2)+';'
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
#4
declare @r varchar(8000)
set @r=''
select @r=@r+rtrim(f1)+'.'+rtrim(f2)+';' from ta where id=@id
select left(@r,len(@r)-1)
set @r=''
select @r=@r+rtrim(f1)+'.'+rtrim(f2)+';' from ta where id=@id
select left(@r,len(@r)-1)
#5
看来都是需要写个函数来解决这个问题,谢谢楼上的各位大虾了。
#6
up ++
#1
用函數
--示例
--建表
create table tb (A varchar(10),B varchar(1000))
Insert into tb
select '111','AA0000,AA0001,AA0002,'
union all select '111','BB0000,BB0001,BB0002,'
union all select '222','AA0120,AA0102,AA1453,'
union all select '222','BB1212,BB3423,BB2123,'
select * from tb
--函數
create function dbo.fn_b(@a varchar(10))
returns varchar(1000)
as
begin
declare @s varchar(1000)
set @s=''
select @s=@s+[B] from tb where A=@a
return (@s)
end
--刪除
drop table tb
drop function dbo.fn_b
--結果
select A,B=dbo.fn_b(A) from tb group by A
A B
-------------------------------------------
111 AA0000,AA0001,AA0002,BB0000,BB0001,BB0002,
222 AA0120,AA0102,AA1453,BB1212,BB3423,BB2123,
--示例
--建表
create table tb (A varchar(10),B varchar(1000))
Insert into tb
select '111','AA0000,AA0001,AA0002,'
union all select '111','BB0000,BB0001,BB0002,'
union all select '222','AA0120,AA0102,AA1453,'
union all select '222','BB1212,BB3423,BB2123,'
select * from tb
--函數
create function dbo.fn_b(@a varchar(10))
returns varchar(1000)
as
begin
declare @s varchar(1000)
set @s=''
select @s=@s+[B] from tb where A=@a
return (@s)
end
--刪除
drop table tb
drop function dbo.fn_b
--結果
select A,B=dbo.fn_b(A) from tb group by A
A B
-------------------------------------------
111 AA0000,AA0001,AA0002,BB0000,BB0001,BB0002,
222 AA0120,AA0102,AA1453,BB1212,BB3423,BB2123,
#2
create table ta(ID nvarchar(2),F1 nvarchar(1),F2 nvarchar(3))
insert ta select '1' , 'A' , 'X1' union all select
'1' , 'B' , 'X2' union all select
'1' ,'C' , 'X3' union all select
'2' , 'A' ,'Y1' union all select
'2' ,'B' ,'Y2'
select * from ta
create function f_str(@id varchar(10))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+rtrim(f2)
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
insert ta select '1' , 'A' , 'X1' union all select
'1' , 'B' , 'X2' union all select
'1' ,'C' , 'X3' union all select
'2' , 'A' ,'Y1' union all select
'2' ,'B' ,'Y2'
select * from ta
create function f_str(@id varchar(10))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+rtrim(f2)
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
#3
create function f_str(@id varchar(10))
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+'.'+rtrim(f2)+';'
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+' '+rtrim(f1)+'.'+rtrim(f2)+';'
from ta where id=@id
return(stuff(@r,1,1,''))
end
go
select id,optionpack=dbo.f_str(id) from ta group by id
#4
declare @r varchar(8000)
set @r=''
select @r=@r+rtrim(f1)+'.'+rtrim(f2)+';' from ta where id=@id
select left(@r,len(@r)-1)
set @r=''
select @r=@r+rtrim(f1)+'.'+rtrim(f2)+';' from ta where id=@id
select left(@r,len(@r)-1)
#5
看来都是需要写个函数来解决这个问题,谢谢楼上的各位大虾了。
#6
up ++