select top 10 from student a inner join class b on a.cid = b.cid group by b.cid order by a.age??
if object_id('student') is not null drop table student go create table student(classid varchar(10), sid int,sage int) insert into student select 'aa', 1, 20 union all select 'aa', 2, 18 union all select 'aa', 3, 19 union all select 'aa', 4, 17 union all select 'aa', 5, 14 union all select 'aa', 6, 12 union all select 'bb', 1, 18 union all select 'bb', 2, 28 union all select 'bb', 3, 38 union all select 'bb', 4, 12 union all select 'bb', 5, 15 union all select 'bb', 6, 18 select * from student t where (select count(*) from student where classid=t.classid and sage<t.sage)<4 ---以最小的4个人为例 order by classid,sage ascclassid sid sage ---------- ----------- ----------- aa 6 12 aa 5 14 aa 4 17 aa 2 18bb 4 12 bb 5 15 bb 6 18 bb 1 18(8 行受影响)
---参考P梁的这个: --------------------------------- -- 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 行受影响) */
if object_id('student') is not null drop table student go create table student(classid varchar(10), sid int,sage int) insert into student select 'aa', 1, 20 union all select 'aa', 2, 18 union all select 'aa', 3, 19 union all select 'aa', 4, 17 union all select 'aa', 5, 14 union all select 'aa', 6, 12 union all select 'bb', 1, 18 union all select 'bb', 2, 28 union all select 'bb', 3, 38 union all select 'bb', 4, 12 union all select 'bb', 5, 15 union all select 'bb', 6, 18 SELECT classid,sid,sage FROM ( SELECT rid=ROW_NUMBER() OVER(PARTITION BY classid ORDER BY sage asc),* FROM student ) AS T WHERE rid<=4 order by 1,3 asc /*classid sid sage ---------- ----------- ----------- aa 6 12 aa 5 14 aa 4 17 aa 2 18 bb 4 12 bb 5 15 bb 6 18 bb 1 18(8 行受影响) */
--考虑到班级不知道多少 ,用动态top语句(top 里面可以写SQL语句)--就这样:搞定!
create table student(stuId varchar(8), stuName varchar(20), age int, ClassId varchar(6));create table class(ClassId varchar(6), ClassName varchar(30)); insert into class(ClassId, ClassName) select '040101','04计算机一班' union all select '040102','04计算机二班' union all select '040201','04工商管理二班' union all select '040202','04工商管理二班' union all select '040203','04工商管理三班'; --drop table student; create table student(stuId varchar(10), stuName varchar(20), age int, ClassId varchar(6)); insert into student(stuId, stuName, age, ClassId) select '040101114','小F', 19, '040101' union all select '040101115','剪子', 22, '040101' union all select '040101116','小梁', 20, '040101' union all select '040101117','大角牛', 24, '040101' union all select '040101118','萧湘', 26, '040101' union all select '040101119','华夏小卒', 14, '040101' union all select '040101120','Jory', 18, '040101' union all select '040101121','John', 20, '040101' union all select '040101122','Bill', 25, '040101' union all select '040101123','Tom', 17, '040101' union all select '040101124','Rosen', 21, '040101' union all select '040101125','Bill rou', 28, '040101' union all select '040201214','Tom1', 19, '040201' union all select '040201215','Jazz2', 22, '040201' union all select '040201216','小梁2', 20, '040201' union all select '040201217','大角牛4', 24, '040201' union all select '040201218','萧湘5', 26, '040201' union all select '040201219','华夏小卒6', 14, '040201' union all select '040201220','Jory8', 18, '040201' union all select '040201221','John008', 20, '040201' union all select '040201222','Bill110', 25, '040201' union all select '040201123','Tom119', 17, '040201' union all select '040201124','Rosen116', 21, '040201' union all select '040201125','Bill rou2', 28, '040201';select * from class; select * from student;select s.stuId, s.stuName, s.age, c.className from student s left join class c on s.ClassId=c.ClassId order by s.ClassId, age;select s.stuId, s.stuName, s.age, c.className from student s left join class c on s.ClassId=c.ClassId where exists( sSELECT classid,stuid,stuName, age FROM ( SELECT rid=ROW_NUMBER() OVER(PARTITION BY classid ORDER BY age asc),* FROM student ) AS T WHERE rid<=10 order by classid,age asc--呵呵:小F的row_number()函数搞定!
--呵呵:过奖 create table student(stuId varchar(8), stuName varchar(20), age int, ClassId varchar(6));create table class(ClassId varchar(6), ClassName varchar(30)); insert into class(ClassId, ClassName) select '040101','04计算机一班' union all select '040102','04计算机二班' union all select '040201','04工商管理二班' union all select '040202','04工商管理二班' union all select '040203','04工商管理三班'; --drop table student; create table student(stuId varchar(10), stuName varchar(20), age int, ClassId varchar(6)); insert into student(stuId, stuName, age, ClassId) select '040101114','小F', 19, '040101' union all select '040101115','剪子', 22, '040101' union all select '040101116','小梁', 20, '040101' union all select '040101117','大角牛', 24, '040101' union all select '040101118','萧湘', 26, '040101' union all select '040101119','华夏小卒', 14, '040101' union all select '040101120','Jory', 18, '040101' union all select '040101121','John', 20, '040101' union all select '040101122','Bill', 25, '040101' union all select '040101123','Tom', 17, '040101' union all select '040101124','Rosen', 21, '040101' union all select '040101125','Bill rou', 28, '040101' union all select '040201214','Tom1', 19, '040201' union all select '040201215','Jazz2', 22, '040201' union all select '040201216','小梁2', 20, '040201' union all select '040201217','大角牛4', 24, '040201' union all select '040201218','萧湘5', 26, '040201' union all select '040201219','华夏小卒6', 14, '040201' union all select '040201220','Jory8', 18, '040201' union all select '040201221','John008', 20, '040201' union all select '040201222','Bill110', 25, '040201' union all select '040201123','Tom119', 17, '040201' union all select '040201124','Rosen116', 21, '040201' union all select '040201125','Bill rou2', 28, '040201';select * from class; select * from student;SELECT classid,stuid,stuName, age FROM ( SELECT rid=ROW_NUMBER() OVER(PARTITION BY classid ORDER BY age asc),* FROM student ) AS T WHERE rid<=10 order by classid,age asc
if object_id('student') is not null drop table student go create table student(classid varchar(10), sid int,sage int) insert into student select 'aa', 1, 20 union all select 'aa', 2, 18 union all select 'aa', 3, 19 union all select 'aa', 4, 17 union all select 'aa', 5, 14 union all select 'aa', 6, 12 union all select 'bb', 1, 18 union all select 'bb', 2, 28 union all select 'bb', 3, 38 union all select 'bb', 4, 12 union all select 'bb', 5, 15 union all select 'bb', 6, 18 go select row_number() over(order by classid) as id , * into #stu from student goselect * from #stu a where id in (select top 4 id from #stu where a.classid = classid order by sage)用的小卒的数据
from student a
inner join class b on a.cid = b.cid
group by b.cid
order by a.age??
if object_id('student') is not null drop table student
go
create table student(classid varchar(10), sid int,sage int)
insert into student select
'aa', 1, 20 union all select
'aa', 2, 18 union all select
'aa', 3, 19 union all select
'aa', 4, 17 union all select
'aa', 5, 14 union all select
'aa', 6, 12 union all select
'bb', 1, 18 union all select
'bb', 2, 28 union all select
'bb', 3, 38 union all select
'bb', 4, 12 union all select
'bb', 5, 15 union all select
'bb', 6, 18 select * from student t
where (select count(*) from student where classid=t.classid and sage<t.sage)<4 ---以最小的4个人为例
order by classid,sage ascclassid sid sage
---------- ----------- -----------
aa 6 12
aa 5 14
aa 4 17
aa 2 18bb 4 12
bb 5 15
bb 6 18
bb 1 18(8 行受影响)
---------------------------------
-- 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 行受影响)
*/
go
create table student(classid varchar(10), sid int,sage int)
insert into student select
'aa', 1, 20 union all select
'aa', 2, 18 union all select
'aa', 3, 19 union all select
'aa', 4, 17 union all select
'aa', 5, 14 union all select
'aa', 6, 12 union all select
'bb', 1, 18 union all select
'bb', 2, 28 union all select
'bb', 3, 38 union all select
'bb', 4, 12 union all select
'bb', 5, 15 union all select
'bb', 6, 18 SELECT classid,sid,sage
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY classid ORDER BY sage asc),*
FROM student
) AS T
WHERE rid<=4
order by 1,3 asc
/*classid sid sage
---------- ----------- -----------
aa 6 12
aa 5 14
aa 4 17
aa 2 18
bb 4 12
bb 5 15
bb 6 18
bb 1 18(8 行受影响)
*/
insert into class(ClassId, ClassName)
select
'040101','04计算机一班' union all select
'040102','04计算机二班' union all select
'040201','04工商管理二班' union all select
'040202','04工商管理二班' union all select
'040203','04工商管理三班';
--drop table student;
create table student(stuId varchar(10), stuName varchar(20), age int, ClassId varchar(6));
insert into student(stuId, stuName, age, ClassId)
select
'040101114','小F', 19, '040101' union all select
'040101115','剪子', 22, '040101' union all select
'040101116','小梁', 20, '040101' union all select
'040101117','大角牛', 24, '040101' union all select
'040101118','萧湘', 26, '040101' union all select
'040101119','华夏小卒', 14, '040101' union all select
'040101120','Jory', 18, '040101' union all select
'040101121','John', 20, '040101' union all select
'040101122','Bill', 25, '040101' union all select
'040101123','Tom', 17, '040101' union all select
'040101124','Rosen', 21, '040101' union all select
'040101125','Bill rou', 28, '040101' union all select
'040201214','Tom1', 19, '040201' union all select
'040201215','Jazz2', 22, '040201' union all select
'040201216','小梁2', 20, '040201' union all select
'040201217','大角牛4', 24, '040201' union all select
'040201218','萧湘5', 26, '040201' union all select
'040201219','华夏小卒6', 14, '040201' union all select
'040201220','Jory8', 18, '040201' union all select
'040201221','John008', 20, '040201' union all select
'040201222','Bill110', 25, '040201' union all select
'040201123','Tom119', 17, '040201' union all select
'040201124','Rosen116', 21, '040201' union all select
'040201125','Bill rou2', 28, '040201';select * from class;
select * from student;select s.stuId, s.stuName, s.age, c.className
from student s left join class c on s.ClassId=c.ClassId
order by s.ClassId, age;select s.stuId, s.stuName, s.age, c.className
from student s left join class c on s.ClassId=c.ClassId
where exists( sSELECT classid,stuid,stuName, age
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY classid ORDER BY age asc),*
FROM student
) AS T
WHERE rid<=10
order by classid,age asc--呵呵:小F的row_number()函数搞定!
create table student(stuId varchar(8), stuName varchar(20), age int, ClassId varchar(6));create table class(ClassId varchar(6), ClassName varchar(30));
insert into class(ClassId, ClassName)
select
'040101','04计算机一班' union all select
'040102','04计算机二班' union all select
'040201','04工商管理二班' union all select
'040202','04工商管理二班' union all select
'040203','04工商管理三班';
--drop table student;
create table student(stuId varchar(10), stuName varchar(20), age int, ClassId varchar(6));
insert into student(stuId, stuName, age, ClassId)
select
'040101114','小F', 19, '040101' union all select
'040101115','剪子', 22, '040101' union all select
'040101116','小梁', 20, '040101' union all select
'040101117','大角牛', 24, '040101' union all select
'040101118','萧湘', 26, '040101' union all select
'040101119','华夏小卒', 14, '040101' union all select
'040101120','Jory', 18, '040101' union all select
'040101121','John', 20, '040101' union all select
'040101122','Bill', 25, '040101' union all select
'040101123','Tom', 17, '040101' union all select
'040101124','Rosen', 21, '040101' union all select
'040101125','Bill rou', 28, '040101' union all select
'040201214','Tom1', 19, '040201' union all select
'040201215','Jazz2', 22, '040201' union all select
'040201216','小梁2', 20, '040201' union all select
'040201217','大角牛4', 24, '040201' union all select
'040201218','萧湘5', 26, '040201' union all select
'040201219','华夏小卒6', 14, '040201' union all select
'040201220','Jory8', 18, '040201' union all select
'040201221','John008', 20, '040201' union all select
'040201222','Bill110', 25, '040201' union all select
'040201123','Tom119', 17, '040201' union all select
'040201124','Rosen116', 21, '040201' union all select
'040201125','Bill rou2', 28, '040201';select * from class;
select * from student;SELECT classid,stuid,stuName, age
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY classid ORDER BY age asc),*
FROM student
) AS T
WHERE rid<=10
order by classid,age asc
if object_id('student') is not null drop table student
go
create table student(classid varchar(10), sid int,sage int)
insert into student select
'aa', 1, 20 union all select
'aa', 2, 18 union all select
'aa', 3, 19 union all select
'aa', 4, 17 union all select
'aa', 5, 14 union all select
'aa', 6, 12 union all select
'bb', 1, 18 union all select
'bb', 2, 28 union all select
'bb', 3, 38 union all select
'bb', 4, 12 union all select
'bb', 5, 15 union all select
'bb', 6, 18
go
select row_number() over(order by classid) as id , * into #stu from student
goselect * from #stu a where id in
(select top 4 id from #stu where a.classid = classid order by sage)用的小卒的数据