create table 你的表(球队 varchar(10),积分 int,净胜球 int,轮数 int)
insert 你的表 values('a',3,       2,      1)
insert 你的表 values('b',1 ,      0 ,     1)
insert 你的表 values('c',3  ,     1  ,    1)
insert 你的表 values('d',1   ,    0   ,   1)
insert 你的表 values('e',0    ,   -1   , 1)insert 你的表 values('a',3 ,      2     , 2)
insert 你的表 values('b',1  ,     0    ,  2)
insert 你的表 values('c',1   ,    0   ,   2)
insert 你的表 values('d',3    ,   2  ,    2)
insert 你的表 values('e',0     ,  -1,     2)insert 你的表 values('a',0       ,-2 ,   3)
insert 你的表 values('b',1 ,      0   ,   3)
insert 你的表 values('c',3  ,     3    ,  3)
insert 你的表 values('d',1   ,    0     , 3)
insert 你的表 values('e',3    ,   1      ,3)insert 你的表 values('a',0,       -2,    4)
insert 你的表 values('b',0 ,      -1 ,   4)
insert 你的表 values('c',3  ,     3  ,    4)
insert 你的表 values('d',1   ,    0   ,   4)
insert 你的表 values('e',3    ,   2    ,  4)select 球队,cast(积分 as varchar(100)) 积分,cast(净胜球 as varchar(100)) 净胜球,轮数,cast('' as varchar(100)) 排名情况 into #临时表 from 你的表 order by 球队,轮数update #临时表 set 积分=(select sum(cast(积分 as int)) from #临时表 tem where tem.球队=#临时表.球队 and tem.轮数<=#临时表.轮数),净胜球=(select sum(cast(净胜球 as int)) from #临时表 tem where tem.球队=#临时表.球队 and tem.轮数<=#临时表.轮数)update #临时表 set 排名情况=(select isnull(sum(1),0)+1 from #临时表 tem where tem.轮数=#临时表.轮数 and tem.球队<>#临时表.球队 and (tem.积分>#临时表.积分 or (tem.积分=#临时表.积分 and tem.净胜球>#临时表.净胜球)))declare @a varchar(100),@b varchar(100),@c varchar(100),@id varchar(10)
select @a='',@b='',@c='',@id=''
update #临时表 set 
@a=case when @id=球队 then @a else '' end+cast(积分 as varchar(10))+',',
@b=case when @id=球队 then @b else '' end+cast(净胜球 as varchar(10))+',',
@c=case when @id=球队 then @c else '' end+cast(排名情况 as varchar(10))+',',
@id=球队,积分=@a,净胜球=@b,排名情况=@cselect 球队,left(得分情况,len(得分情况)-1) 得分情况,left(净胜球情况,len(净胜球情况)-1) 净胜球情况,left(排名情况,len(排名情况)-1) 排名情况 from (
select 球队,max(积分) 得分情况,max(净胜球) 净胜球情况,max(排名情况) 排名情况 from #临时表 group by 球队
) tem
go
drop table 你的表,#临时表

解决方案 »

  1.   

    create table 你的表(球队 varchar(10),积分 int,净胜球 int,轮数 int)
    insert 你的表 values('a',3,       2,      1)
    insert 你的表 values('b',1 ,      0 ,     1)
    insert 你的表 values('c',3  ,     1  ,    1)
    insert 你的表 values('d',1   ,    0   ,   1)
    insert 你的表 values('e',0    ,   -1   , 1)insert 你的表 values('a',3 ,      2     , 2)
    insert 你的表 values('b',1  ,     0    ,  2)
    insert 你的表 values('c',1   ,    0   ,   2)
    insert 你的表 values('d',3    ,   2  ,    2)
    insert 你的表 values('e',0     ,  -1,     2)insert 你的表 values('a',0       ,-2 ,   3)
    insert 你的表 values('b',1 ,      0   ,   3)
    insert 你的表 values('c',3  ,     3    ,  3)
    insert 你的表 values('d',1   ,    0     , 3)
    insert 你的表 values('e',3    ,   1      ,3)insert 你的表 values('a',0,       -2,    4)
    insert 你的表 values('b',0 ,      -1 ,   4)
    insert 你的表 values('c',3  ,     3  ,    4)
    insert 你的表 values('d',1   ,    0   ,   4)
    insert 你的表 values('e',3    ,   2    ,  4)--得到临时表,为下一步做准备
    select 球队,cast(积分 as varchar(100)) 积分,cast(净胜球 as varchar(100)) 净胜球,轮数,cast('' as varchar(100)) 排名情况 into #临时表 from 你的表 order by 球队,轮数--得到得分情况和净胜球情况
    update #临时表 set 积分=(select sum(cast(积分 as int)) from #临时表 tem where tem.球队=#临时表.球队 and tem.轮数<=#临时表.轮数),净胜球=(select sum(cast(净胜球 as int)) from #临时表 tem where tem.球队=#临时表.球队 and tem.轮数<=#临时表.轮数)--得到排名情况
    update #临时表 set 排名情况=(select isnull(sum(1),0)+1 from #临时表 tem where tem.轮数=#临时表.轮数 and tem.球队<>#临时表.球队 and (cast(tem.积分 as int)>cast(#临时表.积分 as int) or (cast(tem.积分 as int)=cast(#临时表.积分 as int) and cast(tem.净胜球 as int)>cast(#临时表.净胜球 as int))))--把得到的结果形成要求的,号分割
    declare @a varchar(100),@b varchar(100),@c varchar(100),@id varchar(10)
    select @a='',@b='',@c='',@id=''
    update #临时表 set 
    @a=case when @id=球队 then @a else '' end+cast(积分 as varchar(10))+',',
    @b=case when @id=球队 then @b else '' end+cast(净胜球 as varchar(10))+',',
    @c=case when @id=球队 then @c else '' end+cast(排名情况 as varchar(10))+',',
    @id=球队,积分=@a,净胜球=@b,排名情况=@cselect 球队,left(得分情况,len(得分情况)-1) 得分情况,left(净胜球情况,len(净胜球情况)-1) 净胜球情况,left(排名情况,len(排名情况)-1) 排名情况 from (
    select 球队,max(积分) 得分情况,max(净胜球) 净胜球情况,max(排名情况) 排名情况 from #临时表 group by 球队
    ) tem
    go
    drop table 你的表,#临时表