一个表格有三列:名字、科目、成绩, 科目中有四门功课,怎么设计查询找出每门功课中成绩 最高的那条信息
想到用group by 可是只能查询功课中最高的成绩,不能显示姓名
select * from 表 where 成绩 in (select max(成绩) in 表 group by 成绩)
declare @名字 nchar(10),@成绩 smallint
select @名字=名字,@成绩=max(成绩) from 表 group by 名字
select * from 表
where 表.名字 = @名字 and 表.成绩 =@成绩
select * from 表 where 成绩 in (select max(成绩) in 表 group by 科目)
select a.名字,a.科目,a.成绩 from 表2 a inner join
(select 名字,max(成绩)as 成绩 from 表2 group by 名字) b
on a.名字 = b.名字 and a.成绩= b.成绩
select * from 表 where 科目+convert(char(5),成绩) in (select 科目+convert(char(5)+max(成绩)) in 表 group by 科目)
select * from (select 名字, 科目, 成绩, rank() over(partition by 科目 order by 成绩 desc) ranking from 表) where ranking = 1;
感谢大家献策啊,呃,这个表……我想起head in first中的一句话,设计糟糕的表应当重新设计,而不应该迁就于更复杂的查询。
select * from
student st,(select max(st.score) max_sc, st.course from student st group by st.course) res
where res.max_sc = st.score and res.course = st.course
select b.* from (select subject,max(score) m from grade GROUP BY subject) t,grade b where t.subject=b.subject and t.m=b.score