create table tb(xue int,chji int) insert tb select 1,90 union all select 2,80 union all select 3,85 union all select 4,87 union all select 5,91 drop table #t select *,id=identity(int) into #t from tb order by chji desc select * from #t where id<4
楼主那么钻牛角尖?既然不想写TOP,那就这样吧:set rowcount 3select * from tb order by 成绩 descset rowcount 0
我是用触发器实现的,你看看吧,表不一样,原理是一样的,改一改就可以了。 功能:查询每个学生分数最高的前己门课程-- ============================================= -- Create function ():创建一个查询每个学生分数最高的前己门课程 --查询的表为选课(学号,课程号,成绩) -- ============================================= create PROC GetTopScores1 @TopN int = 3 -- 此table用来存放每个学生分数最高的前己门课程--table(Xnumber nvarchar(10),Cnumber nvarchar(10),Score char(10)) as begin declare @ScoreTable table (Xnumber nvarchar(10),Cnumber nvarchar(10),Score char(10)) declare @Counter int,@number int,@StartNo int, @Student varchar(10)set @Counter = 0 --set @TopN = 3--此Table用来存放每个学生的数据 declare @StudentTable table (编号 int identity(1,1) not null,学号 nvarchar(10)) --将学生数据存放入Table 变量中 insert @StudentTable select distinct 学号 from 选课--@number 用来存储学生的数目 select @number = count(*) from @StudentTable--此Table 用来存储前100percent 的数据 declare @Top table(编号 int identity(1,1) not null,学号 varchar(10), 课程号 nvarchar(10), 成绩 nvarchar(10))--此循环依次将各个学生的成绩的最高的前几名存入table变量中 while(@Counter < @Number) begin set @Counter = @Counter + 1 select @Student = 学号 from @StudentTable where 编号 = @Counter
insert @Top select 学号,课程号,成绩 from 选课 WHERE 学号 = @Student ORDER BY 成绩 DESC
delete @Top where 编号 > @TopN
insert @ScoreTable select 学号,课程号,成绩 from @Top delete @Top
endend select * from @ScoreTable goexecute dbo.GetTopScores1 5
select * from tb a where (select count(distinct 成绩) from tb where 成绩>=a.成绩)=3
select * from t_stu where 成绩 >= (select max(成绩) from t_stu where 成绩 < (select max(成绩) from t_stu where 成绩 < (select max(成绩) from t_stu where 成绩)))
上边错了! select * from t_stu where 成绩 >= (select max(成绩) from t_stu where 成绩 < (select max(成绩) from t_stu where 成绩 < (select max(成绩) from t_stu )))(小李铅笔刀) 应该: <=3
对,是应该<=3select * from tb a where (select count(distinct 成绩) from tb where 成绩>=a.成绩)<=3
select top 3 with ties * from t order by 成绩 desc
/* 有如下表(t_stu): 学号 成绩 01 90 02 80 03 85 04 87 05 91 … …可不可以用一句sql语句并且不用top求出成绩最好的三个人的信息? */ --生成测试数据 create table t(id int ,score int) insert into t select 01,90 union all select 02,80 union all select 03,85 union all select 04,87 union all select 05,91 go--测试 select top 3 * from t order by score desc--删除测试数据 drop table t--测试结果 5 91 1 90 4 87 --------------------- 原来有人比我先作出来,来晚了啊 ^_^
insert tb select 1,90
union all select 2,80
union all select 3,85
union all select 4,87
union all select 5,91
drop table #t
select *,id=identity(int) into #t from tb order by chji desc
select * from #t where id<4
功能:查询每个学生分数最高的前己门课程-- =============================================
-- Create function ():创建一个查询每个学生分数最高的前己门课程
--查询的表为选课(学号,课程号,成绩)
-- =============================================
create PROC GetTopScores1 @TopN int = 3
-- 此table用来存放每个学生分数最高的前己门课程--table(Xnumber nvarchar(10),Cnumber nvarchar(10),Score char(10))
as
begin
declare @ScoreTable table (Xnumber nvarchar(10),Cnumber nvarchar(10),Score char(10))
declare @Counter int,@number int,@StartNo int, @Student varchar(10)set @Counter = 0
--set @TopN = 3--此Table用来存放每个学生的数据
declare @StudentTable table (编号 int identity(1,1) not null,学号 nvarchar(10))
--将学生数据存放入Table 变量中
insert @StudentTable
select distinct 学号 from 选课--@number 用来存储学生的数目
select @number = count(*) from @StudentTable--此Table 用来存储前100percent 的数据
declare @Top table(编号 int identity(1,1) not null,学号 varchar(10), 课程号 nvarchar(10), 成绩 nvarchar(10))--此循环依次将各个学生的成绩的最高的前几名存入table变量中
while(@Counter < @Number)
begin
set @Counter = @Counter + 1
select @Student = 学号 from @StudentTable where 编号 = @Counter
insert @Top
select 学号,课程号,成绩 from 选课
WHERE 学号 = @Student ORDER BY 成绩 DESC
delete @Top where 编号 > @TopN
insert @ScoreTable
select 学号,课程号,成绩 from @Top delete @Top
endend
select * from @ScoreTable
goexecute dbo.GetTopScores1 5
from tb a
where (select count(distinct 成绩) from tb where 成绩>=a.成绩)=3
from t_stu
where 成绩 >= (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu
where 成绩)))
select *
from t_stu
where 成绩 >= (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu )))(小李铅笔刀) 应该: <=3
from tb a
where (select count(distinct 成绩) from tb where 成绩>=a.成绩)<=3
有如下表(t_stu):
学号 成绩
01 90
02 80
03 85
04 87
05 91
… …可不可以用一句sql语句并且不用top求出成绩最好的三个人的信息?
*/
--生成测试数据
create table t(id int ,score int)
insert into t
select 01,90 union all
select 02,80 union all
select 03,85 union all
select 04,87 union all
select 05,91
go--测试
select top 3 *
from t
order by score desc--删除测试数据
drop table t--测试结果
5 91
1 90
4 87
---------------------
原来有人比我先作出来,来晚了啊
^_^