34,588
社区成员
发帖
与我相关
我的任务
分享
select top ? * from 表 where 班级='班级' and 学科='学科' order by 分数 desc
select top 5/*要几名此填入几*/ * from 表 where 班级='要查的班级' and 学科='要查的学科' order by 分数 desc
select top 1 * from [tb] order by 分數 desc
最快
--声明,借用xiaoF的数据
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([班級] int,[姓名] varchar(2),[學科] varchar(4),[分數] int)
insert [tb]
select 9901,'a1','數學',50 union all
select 9901,'b1','化學',80 union all
select 9901,'c1','物理',82 union all
select 9902,'a2','化學',86 union all
select 9902,'b2','數學',88 union all
select 9902,'c2','物理',90
select 班級,姓名, 學科,分數 from
(
select a.*,id=(select count(1) from tb where [學科]=a.[學科] and [分數]>=a.[分數]) from tb a) t
where t.id<=2--取前两条
order by [學科],[分數] desc--刚才排序出了问题
/*
班級 姓名 學科 分數
----------- ---- ---- -----------
9902 a2 化學 86
9901 b1 化學 80
9902 b2 數學 88
9901 a1 數學 50
9902 c2 物理 90
9901 c1 物理 82
(6 行受影响)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([班級] int,[姓名] varchar(2),[學科] varchar(4),[分數] int)
insert [tb]
select 9901,'a1','數學',50 union all
select 9901,'b1','化學',80 union all
select 9901,'c1','物理',82 union all
select 9902,'a2','化學',86 union all
select 9902,'b2','數學',88 union all
select 9902,'c2','物理',90
select 班級,姓名, 學科,分數 from
(
select a.*,id=(select count(1) from tb where [學科]=a.[學科] and [分數]>=a.[分數]) from tb a) t
where t.id<=2--取前两条
order by t.id desc
/*
班級 姓名 學科 分數
----------- ---- ---- -----------
9901 a1 數學 50
9901 b1 化學 80
9901 c1 物理 82
9902 a2 化學 86
9902 b2 數學 88
9902 c2 物理 90
(6 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-13 10:01:01
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([班級] int,[姓名] varchar(2),[學科] varchar(4),[分數] int)
insert [tb]
select 9901,'a1','數學',50 union all
select 9901,'b1','化學',80 union all
select 9901,'c1','物理',82 union all
select 9902,'a2','化學',86 union all
select 9902,'b2','數學',88 union all
select 9902,'c2','物理',90
--------------开始查询--------------------------
SELECT * FROM tb AS T WHERE 2>(SELECT COUNT(*) FROM tb WHERE 學科=T.學科 AND 分數>T.分數) order by 學科 desc
----------------结果----------------------------
/*班級 姓名 學科 分數
----------- ---- ---- -----------
9901 c1 物理 82
9902 c2 物理 90
9902 b2 數學 88
9901 a1 數學 50
9901 b1 化學 80
9902 a2 化學 86
(所影响的行数为 6 行)
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-13 10:01:01
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([班級] int,[姓名] varchar(2),[學科] varchar(4),[分數] int)
insert [tb]
select 9901,'a1','數學',50 union all
select 9901,'b1','化學',80 union all
select 9901,'c1','物理',82 union all
select 9902,'a2','化學',86 union all
select 9902,'b2','數學',88 union all
select 9902,'c2','物理',90
--------------开始查询--------------------------
select top 3 * from [tb] order by 分數 desc
----------------结果----------------------------
/*班級 姓名 學科 分數
----------- ---- ---- -----------
9902 c2 物理 90
9902 b2 數學 88
9902 a2 化學 86
(所影响的行数为 3 行)
*/
select *
from(
select rnk=dense_rank() over(partition by 班级,学科 ORDER BY 分数 DESC),*
from tb
) as t
where rnk <= 5 --各班各科前5名
---------------------------------
-- Author: liangCK 小梁
-- Title : 查每个分组前N条记录
-- Date : 2008-11-13 17:19:23
---------------------------------
--> 生成测试数据: #T
IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
INSERT INTO #T
SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'
--SQL查询如下:
--按GID分组,查每个分组中Date最新的前2条记录
--1.字段ID唯一时:
SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)
--2.如果ID不唯一时:
SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)
--SQL Server 2005 使用新方法
--3.使用ROW_NUMBER()进行排位分组
SELECT ID,GID,Author,Title,Date
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
FROM #T
) AS T
WHERE rid<=2
--4.使用APPLY
SELECT DISTINCT b.*
FROM #T AS a
CROSS APPLY
(
SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
) AS b
--结果
/*
ID GID Author Title Date
---- ----------- ----------------------------- --------------------------------------- -----------------------
003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000
(6 行受影响)
*/
SELECT 班级,学科,MAX(分数) 分数 FROM TB GROUP BY 班级,学科
??第一名