给个例子给你 create table t
(id int,test varchar(50),Rank int)insert t
select 1,'Hello',1 union all
select 1,'Mr',2 union all
select 1,'King',3 union all
select 2,'I',1 union all
select 2,'am',2 union all
select 2,'Best',4 union all
select 2,'The',3
go
create function f_union(@id int)
returns varchar(100)
as
begin
declare @sql varchar(100)
set @sql=''
select @sql=@sql+' '+test from t where id=@id order by Rank
return(stuff(@sql,1,1,''))
end
goselect id,dbo.f_union(id) as test from t group by id
(id int,test varchar(50),Rank int)insert t
select 1,'Hello',1 union all
select 1,'Mr',2 union all
select 1,'King',3 union all
select 2,'I',1 union all
select 2,'am',2 union all
select 2,'Best',4 union all
select 2,'The',3
go
create function f_union(@id int)
returns varchar(100)
as
begin
declare @sql varchar(100)
set @sql=''
select @sql=@sql+' '+test from t where id=@id order by Rank
return(stuff(@sql,1,1,''))
end
goselect id,dbo.f_union(id) as test from t group by id
属性=(select top 1 属性 from 表 where 编号=A.编号order by newid())
from 表 A
group by 编号
right join
(select 编号,max(属性) as 属性 from 表1 group by 编号)b
on a.编号 = b.编号 and a.属性 = b.属性
编号 varchar(50),
属性 varchar(50)
)
goinsert into 表1
select ' 002','01'
union select ' 002','01'
union select ' 003','02'
union select ' 003','02'
select a.* from 表1 a
right join
(select 编号,max(属性) as 属性 from 表1 group by 编号)b
on a.编号 = b.编号 and a.属性 = b.属性
insert into 表1
select ' 002','01'
union select ' 002','01'
union select ' 003','02'
union select ' 003','02'显示:002
003