id userid ADDRESS date
1 100001 abde 2009-9-18
2 100001 abcde 2009-9-20
3 100001 abde 2009-9-21
10 100002 abcde 2009-8-10
11 100003 abde 2009-9-22
22 100003 abcde 2009-10-30
23 100003 abde 略
24 100004 abcde 略
25 100004 abde 略
26 100005 abcde 2009-1-1
27 100005 abcde 2009-2-1
28 100005 abcde 2009-3-2
29 100005 abcde 2009-3-4
30 100005 abcde 2009-5-4
...............................--结果
id userid ADDRESS date
3 100001 abde 2009-9-21
2 100001 abcde 2009-9-20
10 100002 abcde 2009-8-10
22 100003 abcde 2009-10-30
11 100003 abde 2009-9-22
24 100004 abcde
25 100004 abde
30 100005 abcde 2009-5-4--问题:按userid分组,按date排序.其结果是每userid取两条(没有两条取一条),且总记录取8条.
1 100001 abde 2009-9-18
2 100001 abcde 2009-9-20
3 100001 abde 2009-9-21
10 100002 abcde 2009-8-10
11 100003 abde 2009-9-22
22 100003 abcde 2009-10-30
23 100003 abde 略
24 100004 abcde 略
25 100004 abde 略
26 100005 abcde 2009-1-1
27 100005 abcde 2009-2-1
28 100005 abcde 2009-3-2
29 100005 abcde 2009-3-4
30 100005 abcde 2009-5-4
...............................--结果
id userid ADDRESS date
3 100001 abde 2009-9-21
2 100001 abcde 2009-9-20
10 100002 abcde 2009-8-10
22 100003 abcde 2009-10-30
11 100003 abde 2009-9-22
24 100004 abcde
25 100004 abde
30 100005 abcde 2009-5-4--问题:按userid分组,按date排序.其结果是每userid取两条(没有两条取一条),且总记录取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 行受影响)
*/--得到每组前几条数据
--假設每組Col1中, Col3不會重復--建立測試環境
Create Table TEST
(Col1 Varchar(10),
Col2 Varchar(10),
Col3 Int)
--插入數據
Insert TEST Select 'BD1V','Label', 4
Union All Select 'BD1V', 'BATT', 2
Union All Select 'BD1V', 'ODD', 3
Union All Select 'BD1V', 'HDD', 5
Union All Select 'BD1V', 'LCD', 1
Union All Select 'BD1W','HDD', 3
Union All Select 'BD1W','RAM', 8
Union All Select 'BD1W','TP CABLE', 5
Union All Select 'BD1W','LCD', 6
Union All Select 'BD1W','Label', 2
Union All Select 'BL3', 'LCD CABLE', 7
Union All Select 'BL3', 'LABEL', 6
Union All Select 'BL3', 'LCD', 5
Union All Select 'BL3', 'RAM', 1
Union All Select 'BL3D', 'Label', 4
GO
--測試
--方法一:
Select Col1, Col2, Col3 From TEST A
Where (Select Count(*) From TEST Where Col1 = A.Col1 And Col3 > A.Col3) < 3
Order By Col1, Col3 Desc
--方法二:
Select Col1, Col2, Col3 From TEST A
Where Exists (Select Count(*) From TEST Where Col1 = A.Col1 And Col3 > A.Col3 Having Count(*) < 3)
Order By Col1, Col3 Desc
--方法三:
Select Col1, Col2, Col3 From TEST A
Where Col3 In (Select TOP 3 Col3 From TEST Where Col1 = A.Col1 Order By Col3 Desc)
Order By Col1, Col3 Desc
GO
--刪除測試環境
Drop Table TEST
--結果
/*
Col1 Col2 Col3
BD1V HDD 5
BD1V Label 4
BD1V ODD 3
BD1W RAM 8
BD1W LCD 6
BD1W TP CABLE 5
BL3 LCD CABLE 7
BL3 LABEL 6
BL3 LCD 5
BL3D Label 4
*/
(select *,row_number()over(partition by userid order by date desc) as rn from tb )
where rn<=2
(select *,row_number()over(partition by userid order by date desc) as rn from tb )t
where rn <=2
WHERE [DATE] IN
(SELECT TOP 4 DATE FROM TB WHERE ADDRESS =T.ADDRESS ORDER BY DATE DESC)
-->Author:wufeng4552【水族杰纶】
-->Date :2009-08-26 10:54:45
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[userid] int,[ADDRESS] nvarchar(5),[date] nvarchar(10))
Insert tb
select 1,100001,N'abde',N'2009-9-18' union all
select 2,100001,N'abcde',N'2009-9-20' union all
select 3,100001,N'abde',N'2009-9-21' union all
select 10,100002,N'abcde',N'2009-8-10' union all
select 11,100003,N'abde',N'2009-9-22' union all
select 22,100003,N'abcde',N'2009-10-30' union all
select 23,100003,N'abde',N'略' union all
select 24,100004,N'abcde',N'略' union all
select 25,100004,N'abde',N'略' union all
select 26,100005,N'abcde',N'2009-1-1' union all
select 27,100005,N'abcde',N'2009-2-1' union all
select 28,100005,N'abcde',N'2009-3-2' union all
select 29,100005,N'abcde',N'2009-3-4' union all
select 30,100005,N'abcde',N'2009-5-4'
Go
select * from tb t where id in(
select top 2 id from tb where userid=t.userid order by [date]desc)
/*
id userid ADDRESS date
----------- ----------- ------- ----------
2 100001 abcde 2009-9-20
3 100001 abde 2009-9-21
10 100002 abcde 2009-8-10
11 100003 abde 2009-9-22
23 100003 abde 略
24 100004 abcde 略
25 100004 abde 略
29 100005 abcde 2009-3-4
30 100005 abcde 2009-5-4(9 個資料列受到影響)
*/
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-26 10:57:01
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[userid] int,[ADDRESS] varchar(5),[date] varchar(10))
insert [tb]
select 1,100001,'abde','2009-9-18' union all
select 2,100001,'abcde','2009-9-20' union all
select 3,100001,'abde','2009-9-21' union all
select 10,100002,'abcde','2009-8-10' union all
select 11,100003,'abde','2009-9-22' union all
select 22,100003,'abcde','2009-10-30' union all
select 23,100003,'abde','略' union all
select 24,100004,'abcde','略' union all
select 25,100004,'abde','略' union all
select 26,100005,'abcde','2009-1-1' union all
select 27,100005,'abcde','2009-2-1' union all
select 28,100005,'abcde','2009-3-2' union all
select 29,100005,'abcde','2009-3-4' union all
select 30,100005,'abcde','2009-5-4'
--------------开始查询--------------------------
select
*
from
tb t
where
id
in
(select top 2 id from tb where userid=t.userid order by [date]desc)----------------结果----------------------------
/*id userid ADDRESS date
----------- ----------- ------- ----------
2 100001 abcde 2009-9-20
3 100001 abde 2009-9-21
10 100002 abcde 2009-8-10
11 100003 abde 2009-9-22
23 100003 abde 略
24 100004 abcde 略
25 100004 abde 略
29 100005 abcde 2009-3-4
30 100005 abcde 2009-5-4(9 行受影响)
*/
这也不动脑?.SELECT TOP 8 *
FROM (
select
*
from
tb t
where
id
in
(select top 2 id from tb where userid=t.userid order by [date]desc)) AS A
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-26 10:57:01
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[userid] int,[ADDRESS] varchar(5),[date] varchar(10))
insert [tb]
select 1,100001,'abde','2009-9-18' union all
select 2,100001,'abcde','2009-9-20' union all
select 3,100001,'abde','2009-9-21' union all
select 10,100002,'abcde','2009-8-10' union all
select 11,100003,'abde','2009-9-22' union all
select 22,100003,'abcde','2009-10-30' union all
select 23,100003,'abde','略' union all
select 24,100004,'abcde','略' union all
select 25,100004,'abde','略' union all
select 26,100005,'abcde','2009-1-1' union all
select 27,100005,'abcde','2009-2-1' union all
select 28,100005,'abcde','2009-3-2' union all
select 29,100005,'abcde','2009-3-4' union all
select 30,100005,'abcde','2009-5-4'
--------------开始查询--------------------------
select
top 8 * from
(
select
*
from
tb t
where
id
in
(select top 2 id from tb where userid=t.userid order by [date]desc)
)t
/*id userid ADDRESS date
----------- ----------- ------- ----------
2 100001 abcde 2009-9-20
3 100001 abde 2009-9-21
10 100002 abcde 2009-8-10
11 100003 abde 2009-9-22
23 100003 abde 略
24 100004 abcde 略
25 100004 abde 略
29 100005 abcde 2009-3-4(8 行受影响)
*/
go
create table [tb]([id] int,[userid] int,[ADDRESS] varchar(5),[date] varchar(10))
insert [tb]
select 1,100001,'abde','2009-9-18' union all
select 2,100001,'abcde','2009-9-20' union all
select 3,100001,'abde','2009-9-21' union all
select 10,100002,'abcde','2009-8-10' union all
select 11,100003,'abde','2009-9-22' union all
select 22,100003,'abcde','2009-10-30' union all
select 23,100003,'abde','略' union all
select 24,100004,'abcde','略' union all
select 25,100004,'abde','略' union all
select 26,100005,'abcde','2009-1-1' union all
select 27,100005,'abcde','2009-2-1' union all
select 28,100005,'abcde','2009-3-2' union all
select 29,100005,'abcde','2009-3-4' union all
select 30,100005,'abcde','2009-5-4'
go
select top 8 id,userid,ADDRESS,date
from (
select *,rn=row_number()over(partition by userid order by date desc)
from tb
) as t
where rn <=2
/*
id userid ADDRESS date
----------- ----------- ------- ----------
3 100001 abde 2009-9-21
2 100001 abcde 2009-9-20
10 100002 abcde 2009-8-10
23 100003 abde 略
11 100003 abde 2009-9-22
24 100004 abcde 略
25 100004 abde 略
30 100005 abcde 2009-5-4(8 行受影响)
*/05的这样应该会快些