当前位置: 代码迷 >> Sql Server >> 跑求:死了:小弟我想得到一个表值不是最大记录 怎么做
  详细解决方案

跑求:死了:小弟我想得到一个表值不是最大记录 怎么做

热度:466   发布时间:2016-04-27 16:42:42.0
跑求:急死了:我想得到一个表值不是最大记录 如何做?
我只知道   select   max(******)   可以得到最大值   但是我想得到   不是最大值的如何做?  
id     course         成绩
001     英语           80
001     英语           65
001     英语           90
我就想得到
id     course         成绩
001     英语           80
001     英语           65
帮忙啊!谢谢了

------解决方案--------------------

2005:
create table ta(id int,name int)
insert ta select 1,2 union all select 1,3


select *,row=ROW_NUMBER () over (order by id)
from ta

2000只有用临时表/新增显示列实现
create table ta(id int,name int)
insert ta select 1,2
union all select 1,3

select *,自编号=1 into # from ta
declare @i int
set @i=0
update # set [email protected],@[email protected]+1
查询:
select * from #
------解决方案--------------------
--如果只是course做比較的話

declare @ta table(id char(3), course Nvarchar(10), 成绩 int)
insert @ta select '001 ', N '英语 ', 80
union all select '001 ', N '英语 ', 65
union all select '001 ', N '英语 ', 90

select * from @ta A Where 成绩 Not In (Select Max(成绩) From @ta Where course = A.course)

--Result
/*
id course 成绩
001 英语 80
001 英语 65
*/


------解决方案--------------------
--不用临时表

select *
from 表名 a
where 成绩 not in (select max(成绩) from 表名 where course = a.course and id = a.id)
  相关解决方案