日期:2014-05-17  浏览次数:20635 次

求大哥们帮写这条简单的SQL语句(我在线等你们)
帮大哥们写好了创建表的代码

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 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(姓名,'
如何在时间段找出特定周几数据