Insert test
Select
'000102100480', 60
union all select
'000102100579', 72
union all select
'000102100579', 67
union all select
'000102101272', 60
union all select
'000102101272', 34
000102101272 60 34
Select * from #test
--创建一个处理函数
Create function uf_functionName(
@id varchar(30),
@score integer
)returns varchar(8000)
as
begin
declare @re varchar(8000)
set @re=''
if @score=0
select @re=@re+' '+cast(score as varchar)
from test
where id=@id
else
select @re=@re+' '+cast(score as varchar)
from test
where id=@id
return(stuff(@re,1,1,''))
end
go
--调用函数实现查询
select distinct id,score=dbo.uf_functionName(id,score)
from test
group by id,score
go