日期:2014-05-19  浏览次数:20371 次

请教一条统计存储过程!
有表:SSJC   结构:
  id         roomid         score         xb  
  1               101             85             jsj  
  2               102               85           jsj
3                 205             90               hg
4                 206             70               hg

由上表统计结果插入下表:
统计表:SSCJ_Conts结构  

id       xb       avg       num
1         jsj     85             1
2           hg       80           2  

注:(   num   名次)

问   :   如何用存储过程实现以上统计功能  


------解决方案--------------------
declare @t table (id int,roomid int,score int,xb varchar(20))
insert into @t
select 1, 101, 85, 'jsj '
union all select 2, 102, 85, 'jsj '
union all select 3, 205, 90, 'hg '
union all select 4, 206, 70, 'hg '

select xb, avg(score) as avg,num=identity(int,1,1) into #tt from @t group by xb order by avg(score) desc

select * from #tt
/*
xb avg num
--------------------------------------------------
jsj 85 2
hg 80 1
*/