帮忙写条语句,谢谢了。col1 col21 a
2 b
3 c
4 c
5 a
6 a
7 b
8 c
9 c
10 a
11 a
12 b
13 c
14 b
15 b
16 a提取按col2分组的每个col1数据按升序排的前三位.col1为int型
结果应该是
1 a
5 a
6 a
2 b
7 b
12 b
3 c
4 c
8 c
2 b
3 c
4 c
5 a
6 a
7 b
8 c
9 c
10 a
11 a
12 b
13 c
14 b
15 b
16 a提取按col2分组的每个col1数据按升序排的前三位.col1为int型
结果应该是
1 a
5 a
6 a
2 b
7 b
12 b
3 c
4 c
8 c
解决方案 »
- 服务器时间问题
- 邹建大侠请进--数据库压力问题
- 十万火急送分!SQL SERVER升级到Oracle,里面的存储过程能用吗?
- net2005+sql2008 连接字符串错误 error: 26 定位指定的服务器/实例时出错的问题?
- sql function执行效率
- 服务器: 消息 245,级别 16,状态 1,行 1将 varchar 值 '41----' 转换为数据类型为 int 的列时发生语法错误。
- 请那位大虾帮忙,怎样在当前数据库中用SQL语句修改另一数据库中的某个表的名称???
- 请问。。(BCB6+SQLSERVER2000)在线等。。。。。
- 高手帮我看看,询的结果建立一张表
- 急急急!!!执行DBCC DBREINDEX 会不会导致数据库数据丢失?(有示例)
- 疑惑麻烦来解惑
- 疑惑麻烦来解惑
SELECT TOP 3 FROM TB WHERE COL2=T.COL2 ORDER BY COL1 ASC)ORDER BY COL1 ASC
from tb a
where col1 in (select top 3 col1 from tb where a.col2=col2 order by col1)
order by col2
SELECT TOP 3 FROM TB WHERE COL2=T.COL2 ORDER BY COL1 ASC)ORDER BY COL2 ASC
--> Author : wufeng4552
--> Date : 2009-11-10 14:33:17
if object_id('tempdb.dbo.#t') is not null drop table #t
go
create table #t (col1 int,col2 nvarchar(2))
insert into #t
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'c' union all
select 5,'a' union all
select 6,'a' union all
select 7,'b' union all
select 8,'c' union all
select 9,'c' union all
select 10,'a' union all
select 11,'a' union all
select 12,'b' union all
select 13,'c' union all
select 14,'b' union all
select 15,'b' union all
select 16,'a'
select * from #t t where col1 in(select top 3 col1 from #T where col2=t.col2 order by col1)
order by col2,col1
/*
col1 col2
----------- ----
1 a
5 a
6 a
2 b
7 b
12 b
3 c
4 c
8 c(9 個資料列受到影響)
*/
-- Author:SQL77--RICHIE
-- Version:V1.001 Date:2008-05-15--转Flystone*/-- Test Data: TB
If object_id('TB') is not null
Drop table TB
Go
Create table TB(col1 int,col2 varchar(1))
Go
Insert into TB
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'c' union all
select 5,'a' union all
select 6,'a' union all
select 7,'b' union all
select 8,'c' union all
select 9,'c' union all
select 10,'a' union all
select 11,'a' union all
select 12,'b' union all
select 13,'c' union all
select 14,'b' union all
select 15,'b' union all
select 16,'a'
Go
--StartSELECT * FROM TB T WHERE COL1 IN(
SELECT TOP 3 COL1 FROM TB WHERE COL2=T.COL2 ORDER BY COL1 ASC)ORDER BY COL2 ASC
--Result:
/*(所影响的行数为 16 行)col1 col2
----------- ----
1 a
5 a
6 a
7 b
2 b
12 b
3 c
4 c
8 c(所影响的行数为 9 行)*/
--End
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-10 14:43:14
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col1] int,[col2] varchar(1))
insert [tb]
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'c' union all
select 5,'a' union all
select 6,'a' union all
select 7,'b' union all
select 8,'c' union all
select 9,'c' union all
select 10,'a' union all
select 11,'a' union all
select 12,'b' union all
select 13,'c' union all
select 14,'b' union all
select 15,'b' union all
select 16,'a'
--------------开始查询--------------------------
select
*
from
tb t
where
col1 in(select top 3 col1 from tb where col2=t.col2 order by col1)
order by
col2,col1----------------结果----------------------------
/*
(16 行受影响)
col1 col2
----------- ----
1 a
5 a
6 a
2 b
7 b
12 b
3 c
4 c
8 c(9 行受影响)
*/
-- 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 行受影响)
*/
insert into tb select
1,'a' union all select
2,'b' union all select
3,'c' union all select
4,'c' union all select
5,'a' union all select
6,'a' union all select
7,'b' union all select
8,'c' union all select
9,'c' union all select
10,'a' union all select
11,'a' union all select
12,'b' union all select
13,'c' union all select
14,'b' union all select
15,'b' union all select
16,'a' select Col1,Col2
from (select distinct col2 from tb) A
Cross apply (select top 3 col1 from tb where a.col2=col2 order by col1) B
/*Col1 Col2
----------- ----------
1 a
5 a
6 a
2 b
7 b
12 b
3 c
4 c
8 c(9 行受影响)
*/
drop table tb