帮大哥们写好了创建表的代码
create table #tableTest
(ID int identity,姓名 varchar(20),报读科目 varchar(20),费用 money)
go
insert into #tableTest
select '小明','语文',300 union all
select '小明','数学',500 union all
select '小明','英语',500 union all
select '小红','语文',300 union all
select '小红','数学',500
go
select *from #tableTest
现在的表的结果是这样的

我要的结果是如下图的

------解决方案--------------------
create table tableTest
(ID int identity,姓名 varchar(20),报读科目 varchar(20),费用 money)
go
insert into tableTest
select '小明','语文',300 union all
select '小明','数学',500 union all
select '小明','英语',500 union all
select '小红','语文',300 union all
select '小红','数学',500
go
select *from tableTest
create function dbo.fn_mergeSTR(@name varchar(20),@split varchar(10))
returns varchar(300)
as
begin
declare @str varchar(300);
set @str = '';
select @str = @str + 报读科目 + @split
from tableTest
where 姓名 = @name
set @str = left(@str , len(@str) - LEN(@split) )
return @str --返回值
end
go
select 姓名,
报读科目,
SUM(费用) as 费用
from
(
select 姓名,
dbo.fn_mergeSTR(姓名,'
------解决方案--------------------
') as 报读科目,
费用
from tableTest
) t
group by 姓名,
报读科目
order by 3 desc
/*
姓名 报读科目 费用
小明 语文
------解决方案--------------------
数学
------解决方案--------------------
英语 1300.00
小红 语文
------解决方案--------------------
数学 800.00
*/