软件下载吧文章资讯

分类分类

SQL Server实现查询每个分组的前N条记录

2024-02-28 13:28作者:下载吧

SQL语句查询每个分组的前N条记录的实现方法:

1、生成测试数据: #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’
union all
select ‘010’, 4, ‘黄蛋蛋’, ‘SQL技术手册蛋蛋’, ‘2006-01-01’;

展开全部

相关文章

说两句网友评论
    我要跟贴
    取消