27,581
社区成员




select s.sid,1+(
select count(score) from sc where sc.sid!=s.sid group by sc.sid
having avg(sc.score)<(select avg(sc.score) from sc where s.sid=sc.sid)
) 名次
from sc s;
select t1.SID , t1.SCORE 平均成绩 , t2.px 名次 from
(select SID , avg(SCORE) SCORE from sc group by SID) t1,
(select m.* , px = (select count(1) from (select SID , avg(SCORE) SCORE from sc group by SID) n where SCORE > t.SCORE) + 1 from (select SID , avg(SCORE) SCORE from sc group by SID) m) t2
where t1.sid = t2.sid
create table t(SID int, CID int,SCORE int )
insert t select
1, 11,70 union all select
2, 11,60 union all select
3, 11,50 union all select
4, 11,80 union all select
5, 11,90 union all select
1, 12,60 union all select
2, 12,60 union all select
3, 12,60 union all select
4, 12,60 union all select
5, 12,60
select sid,score=avg(score) into #t
from t
group by sid
select sid,score,pm=(select count(*) from #t where score>= t.score)
from #t t
sid score pm
----------- ----------- -----------
1 65 3
2 60 4
3 55 5
4 70 2
5 75 1
(5 行受影响)
drop table #t
drop table t
SELECT
SID,AVG_score,
(SELECT COUNT(*) FROM (SELECT A.SID,AVG(A.score) AVG_score FROM TB A GROUP BY A.SID)AS T WHERE T.AVG_score>=T1.AVG_score) AS NUM
FROM
(SELECT A.SID,AVG(A.score) AVG_score FROM TB A GROUP BY A.SID)AS T1
select
a.SID,a.score as avg_score,
(select count(1)+1 from (select sid,avg(score) as score from sc group by sid) b where a.sid=b.sid and a.score<b.score)
from
(select sid,avg(score) as score from sc group by sid) a