本帖最后由 liyihongcug 于 2014-6-9 11:36 编辑
SQL查询每个分组的前N条记录
IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T http://hi.baidu.com/baoaoyu/item/314a7a3bb4cb7942023edc6c
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)
--SQLServer 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
select dateadd(minute,-10,GETDATE()) // nandian queshi mafan view laijiejue
ate table a(dt datetime)
insert into a(dt)
select '2012-12-21 14:19'
union
select '2012-12-21 14:24'
union
select '2012-12-21 14:28'
union
select '2012-12-21 14:39'
union
select '2012-12-21 14:44'
union
select '2012-12-21 14:52'
需要的结果:
'2012-12-21 14:19'
'2012-12-21 14:39'
'2012-12-21 14:52'
或者查询出表中所有数据,将记录标识为是否有效,
(sql 2000版本 )
2012-12-21 14:19:00.000 有效
2012-12-21 14:24:00.000 无效
2012-12-21 14:28:00.000 无效
2012-12-21 14:39:00.000 有效
2012-12-21 14:44:00.000 无效
2012-12-21 14:52:00.000 有效
------解决方案--------------------
这样写要简单点
DECLARE @minDt DATETIME
SET @minDt=(SELECT MIN(dt) FROM a)
SELECT MIN(dt) AS dt FROM a
GROUP BY DATEDIFF(mi,@minDt,dt)/10
------解决方案--------------------
select a.dt,case isnull(b.dt,'12345') when '12345' then '无效' else '有效' end from a left join
(
select dt from (
select DATEDIFF(mi,'1990-01-01 00:00:00',dt)/10 as dt from a)a
group by dt having count(1)=1
)b on DATEDIFF(mi,'1990-01-01 00:00:00',a.dt)/10=b.dt
------解决方案--------------------
--1
SELECT MIN(dt)
FROM (
SELECT dt , DATEDIFF (mi , (SELECT MIN (dt) FROM a) , a.dt)/ 10 AS d FROM a
) t
GROUP BY d
--2
SELECT a.dt ,CASE WHEN t.dt IS NULL THEN '无效' ELSE '有效' END
from
(
SELECT MIN(dt) AS dt
FROM (
SELECT dt , DATEDIFF (mi , (SELECT MIN (dt) FROM a) , a.dt)/ 10 AS d FROM a
) t
GROUP BY d
) t RIGHT JOIN a ON t.dt=a.dt http://www.weste.net/2008/11-7/2108072583.html
oogle http://203.208.46.144/