--test
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([CityName] varchar(1),[Airpoar] varchar(2))
insert [tb]
select 'A','A1' union all
select 'A','A2' union all
select 'B','B1' union all
select 'C','C1' union all
select 'D','D1' union all
select 'D','D2' union all
select 'D','D3' union all
select 'D','D4'
--把相同的列值合并成一行
select
CityName,
stuff((select ','+Airpoar from tb where CityName=t.CityName for xml path('')),1,1,'') as Airpoar
from
tb t
group by
CityName
--函数
create function f_strUnit(@cityname varchar)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+airpoar from tb where cityname=@cityname
return @str
end
--把相同的列值合并成一行
declare @str varchar(8000)
set @str=''
select @str=@str+','+airpoar from tb where cityname='D'
浙公网安备 33010602011771号