随风而去

博客园 首页 新随笔 联系 订阅 管理
declare @test table(
    tNo            
int,
    tCourse        
varchar(10),
    tResult        numeric(
10,1))

insert into @test values(1001,'语文',80.5)
insert into @test values(1002,'英语',100)
insert into @test values(1003,'语文',78.5)
insert into @test values(1002,'数学',90.5)
insert into @test values(1003,'数学',123.5)
insert into @test values(1002,'语文',102.5)
insert into @test values(1001,'英语',92)
insert into @test values(1003,'英语',88)
insert into @test values(1001,'数学',134)

select tNo,
    
'语文'=isnull(sum(case when tCourse='语文' then tResult end),0),
    
'英语'=isnull(sum(case when tCourse='英语' then tResult end),0),
    
'数学'=isnull(sum(case when tCourse='数学' then tResult end),0)
from @test
group by tNo
/*--------------------------------------
tNo         语文                                      英语                                      数学
----------- --------------------------------------- --------------------------------
1001        80.5                                    92.0                                    134.0
1002        102.5                                  100.0                                   90.5
1003        78.5                                    88.0                                    123.5
-------------------------------------------------
*/

posted on 2008-07-08 16:57  Aaron_Zhang  阅读(241)  评论(0)    收藏  举报