-- =============================================
-- 作者: Rising_Sun>
-- 创建日期: 2012-9-28>
-- 描述: 显示每个类别下最新更新的n条数据>
-- =============================================
DECLARE @t TABLE
(
NewsId INT,
NewsType NVARCHAR(20),
NewsTitle NVARCHAR(50),
AddTime DATETIME
)
INSERT INTO @t VALUES(1,'体育新闻','火箭赢了','2011-10-11')
INSERT INTO @t VALUES(2,'体育新闻','刘翔因伤退出','2011-10-12')
INSERT INTO @t VALUES(3,'体育新闻','奥尼尔减肥成功','2011-10-13')
INSERT INTO @t VALUES(4,'八卦新闻','凤姐结婚了','2011-10-11')
INSERT INTO @t VALUES(5,'八卦新闻','刘德华当爹了','2011-10-12')
INSERT INTO @t VALUES(6,'八卦新闻','苍老师来中国宣传了','2011-10-13')
INSERT INTO @t VALUES(7,'财经新闻','股票跌破2800点','2011-10-12')
INSERT INTO @t VALUES(8,'财经新闻','人民币首次降息','2011-10-10')
--每个类别最新显示数量
DECLARE @ShowNums INT
SET @ShowNums=2
SELECT * FROM
(
SELECT NewsId,NewsTitle,NewsType,AddTime,
ROW_NUMBER() OVER(PARTITION BY NewsType ORDER BY AddTime DESC) AS RowIndex
FROM @t
) AS T
WHERE RowIndex =@ShowNums