表结构如下:
item item_name receive_qty, receive_date
aaa cpu 100 2009-1-1
aaa cpu 200 2009-2-1
aaa cpu 150 2009-3-1
aaa cpu 250 2009-4-1
bbb memory 100 2009-6-1
bbb memory 210 2009-7-1
bbb memory 150 2009-8-1
bbb memory 200 2009-9-1想要得到的结果是按时间倒序排序,取最近的三条记录,
item item_name receive_qty, receive_date
aaa cpu 250 2009-4-1
aaa cpu 150 2009-3-1
aaa cpu 200 2009-2-1
bbb memory 200 2009-9-1
bbb memory 150 2009-8-1
bbb memory 210 2009-7-1请达人告知查询语句,谢谢。
item item_name receive_qty, receive_date
aaa cpu 100 2009-1-1
aaa cpu 200 2009-2-1
aaa cpu 150 2009-3-1
aaa cpu 250 2009-4-1
bbb memory 100 2009-6-1
bbb memory 210 2009-7-1
bbb memory 150 2009-8-1
bbb memory 200 2009-9-1想要得到的结果是按时间倒序排序,取最近的三条记录,
item item_name receive_qty, receive_date
aaa cpu 250 2009-4-1
aaa cpu 150 2009-3-1
aaa cpu 200 2009-2-1
bbb memory 200 2009-9-1
bbb memory 150 2009-8-1
bbb memory 210 2009-7-1请达人告知查询语句,谢谢。
IN (SELECT TOP 3 receive_date FROM TB WHERE ITEM=T.ITEM ORDER BY receive_date DESC)
IN (SELECT TOP 3 receive_date FROM TB WHERE ITEM=T.ITEM ORDER BY receive_date DESC)
ORDER BY receive_date DESC再加一下排序这个等他们拿帖来,经典的
-- 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 行受影响)
*/
declare @tb table(item varchar(5), item_name varchar(10), receive_qty int, receive_date datetime)
insert @tb select
'aaa' ,'cpu', 100 , '2009-1-1' union all select
'aaa' ,'cpu' , 200 , '2009-2-1' union all select
'aaa' ,'cpu' , 150 , '2009-3-1' union all select
'aaa' , 'cpu' , 250 , '2009-4-1' union all select
'bbb' , 'memory' , 100 , '2009-6-1' union all select
'bbb' , 'memory' , 210 , '2009-7-1' union all select
'bbb' , 'memory' , 150 , '2009-8-1' union all select
'bbb' , 'memory' ,200, '2009-9-1' select * from @tb t
where 3>(select count(1) from @tb where item=t.item and receive_date>t.receive_date)item item_name receive_qty receive_date
----- ---------- ----------- -----------------------
aaa cpu 200 2009-02-01 00:00:00.000
aaa cpu 150 2009-03-01 00:00:00.000
aaa cpu 250 2009-04-01 00:00:00.000
bbb memory 210 2009-07-01 00:00:00.000
bbb memory 150 2009-08-01 00:00:00.000
bbb memory 200 2009-09-01 00:00:00.000(6 行受影响)
insert @tb select
'aaa' ,'cpu', 100 , '2009-1-1' union all select
'aaa' ,'cpu' , 200 , '2009-2-1' union all select
'aaa' ,'cpu' , 150 , '2009-3-1' union all select
'aaa' , 'cpu' , 250 , '2009-4-1' union all select
'bbb' , 'memory' , 100 , '2009-6-1' union all select
'bbb' , 'memory' , 210 , '2009-7-1' union all select
'bbb' , 'memory' , 150 , '2009-8-1' union all select
'bbb' , 'memory' ,200, '2009-9-1'SELECT * FROM @tb T WHERE receive_date
IN (SELECT TOP 3 receive_date FROM @tb WHERE ITEM=T.ITEM ORDER BY receive_date DESC)
ORDER BY item,receive_date DESC
(所影响的行数为 8 行)item item_name receive_qty receive_date
----- ---------- ----------- ------------------------------------------------------
aaa cpu 250 2009-04-01 00:00:00.000
aaa cpu 150 2009-03-01 00:00:00.000
aaa cpu 200 2009-02-01 00:00:00.000
bbb memory 200 2009-09-01 00:00:00.000
bbb memory 150 2009-08-01 00:00:00.000
bbb memory 210 2009-07-01 00:00:00.000(所影响的行数为 6 行)借小卒数据用下,加个排序,晕了,上面排序还是没弄对
declare @tb table(item varchar(5), item_name varchar(10), receive_qty int, receive_date datetime)
insert @tb select
'aaa' ,'cpu', 100 , '2009-1-1' union all select
'aaa' ,'cpu' , 200 , '2009-2-1' union all select
'aaa' ,'cpu' , 150 , '2009-3-1' union all select
'aaa' , 'cpu' , 250 , '2009-4-1' union all select
'bbb' , 'memory' , 100 , '2009-6-1' union all select
'bbb' , 'memory' , 210 , '2009-7-1' union all select
'bbb' , 'memory' , 150 , '2009-8-1' union all select
'bbb' , 'memory' ,200, '2009-9-1' select * from @tb t
where (select count(1) from @tb where item=t.item and receive_date>t.receive_date)<3
order by item asc,receive_date descitem item_name receive_qty receive_date
----- ---------- ----------- -----------------------
aaa cpu 250 2009-04-01 00:00:00.000
aaa cpu 150 2009-03-01 00:00:00.000
aaa cpu 200 2009-02-01 00:00:00.000
bbb memory 200 2009-09-01 00:00:00.000
bbb memory 150 2009-08-01 00:00:00.000
bbb memory 210 2009-07-01 00:00:00.000(6 行受影响)
insert @tb select
'aaa' ,'cpu', 100 , '2009-1-1' union all select
'aaa' ,'cpu' , 200 , '2009-2-1' union all select
'aaa' ,'cpu' , 150 , '2009-3-1' union all select
'aaa' , 'cpu' , 250 , '2009-4-1' union all select
'bbb' , 'memory' , 100 , '2009-6-1' union all select
'bbb' , 'memory' , 210 , '2009-7-1' union all select
'bbb' , 'memory' , 150 , '2009-8-1' union all select
'bbb' , 'memory' ,200, '2009-9-1'
;with hgo as
(
select *,row_number() over(partition by item order by receive_date desc) rank from @tb
)
select item,item_name,receive_qty,receive_date from hgo where rank<=3
item item_name receive_qty receive_date
----- ---------- ----------- -----------------------
aaa cpu 250 2009-04-01 00:00:00.000
aaa cpu 150 2009-03-01 00:00:00.000
aaa cpu 200 2009-02-01 00:00:00.000
bbb memory 200 2009-09-01 00:00:00.000
bbb memory 150 2009-08-01 00:00:00.000
bbb memory 210 2009-07-01 00:00:00.000
insert @tb select
'aaa' ,'cpu', 100 , '2009-1-1' union all select
'aaa' ,'cpu' , 200 , '2009-2-1' union all select
'aaa' ,'cpu' , 150 , '2009-3-1' union all select
'aaa' , 'cpu' , 250 , '2009-4-1' union all select
'bbb' , 'memory' , 100 , '2009-6-1' union all select
'bbb' , 'memory' , 210 , '2009-7-1' union all select
'bbb' , 'memory' , 150 , '2009-8-1' union all select
'bbb' , 'memory' ,200, '2009-9-1'--(1)
;with hgo as
(
select *,row_number() over(partition by item order by receive_date desc) rank from @tb
)
select item,item_name,receive_qty,receive_date from hgo where rank<=3--(2)
select * from @tb B where receive_date not in
(
select receive_date from @tb B where not exists(select * from @tb where item=B.item and receive_date<B.receive_date)
) order by item,receive_date desc,receive_qtyitem item_name receive_qty receive_date
----- ---------- ----------- -----------------------
aaa cpu 250 2009-04-01 00:00:00.000
aaa cpu 150 2009-03-01 00:00:00.000
aaa cpu 200 2009-02-01 00:00:00.000
bbb memory 200 2009-09-01 00:00:00.000
bbb memory 150 2009-08-01 00:00:00.000
bbb memory 210 2009-07-01 00:00:00.000aa
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-09-06 16:30:56
-- 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.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([item] varchar(3),[item_name] varchar(6),[receive_qty] int,[receive_date] datetime)
insert [tb]
select 'aaa','cpu',100,'2009-1-1' union all
select 'aaa','cpu',200,'2009-2-1' union all
select 'aaa','cpu',150,'2009-3-1' union all
select 'aaa','cpu',250,'2009-4-1' union all
select 'bbb','memory',100,'2009-6-1' union all
select 'bbb','memory',210,'2009-7-1' union all
select 'bbb','memory',150,'2009-8-1' union all
select 'bbb','memory',200,'2009-9-1'
--------------开始查询--------------------------
--2000
select
*
from
tb t
where
(select count(1) from tb where item=t.item and receive_date>t.receive_date)<3
order by
item asc,receive_date desc---2000
SELECT
*
FROM
tb T
WHERE
receive_date
IN
(SELECT TOP 3 receive_date FROM tb WHERE ITEM=T.ITEM ORDER BY receive_date DESC)
ORDER BY
item,receive_date DESC
--2005
;with f as
(
select *,row_number() over(partition by item order by receive_date desc) rank from tb
)
select item,item_name,receive_qty,receive_date from f where rank<=3
----------------结果----------------------------
/* item item_name receive_qty receive_date
---- --------- ----------- -----------------------
aaa cpu 250 2009-04-01 00:00:00.000
aaa cpu 150 2009-03-01 00:00:00.000
aaa cpu 200 2009-02-01 00:00:00.000
bbb memory 200 2009-09-01 00:00:00.000
bbb memory 150 2009-08-01 00:00:00.000
bbb memory 210 2009-07-01 00:00:00.000
*/