SELECT  NewsID,CategoryID,CreateTime FROM Public_News a WHERE (     SELECT count(*) FROM Public_News b WHERE b.CategoryID=a.CategoryID AND 
b.newsID>a.newsID  )<5 order by CategoryID,CreateTime desc
 

解决方案 »

  1.   


    declare @Public_News table (NewsID int,CategoryID int,CreateTime datetime)
    insert into @Public_News
    select 1,2,'2009-02-12' union all
    select 2,17,'2009-02-12' union all
    select 3,15,'2009-02-12' union all
    select 4,1,'2009-02-12' union all
    select 5,7,'2009-02-12' union all
    select 6,6,'2009-02-12' union all
    select 7,5,'2009-02-12' union all
    select 8,4,'2009-02-12' union all
    select 9,1,'2009-02-12' union all
    select 10,2,'2009-02-12' union all
    select 11,2,'2009-02-12' union all
    select 12,2,'2009-02-12' union all
    select 13,2,'2009-02-12' union all
    select 14,2,'2009-02-12' union all
    select 15,2,'2009-02-12' union all
    select 15,2,'2009-02-12' union all
    select 15,2,'2009-02-12' union all
    select 15,2,'2009-02-12' union all
    select 15,2,'2009-02-12' union all
    select 15,2,'2009-02-12' union all
    select 16,3,'2009-02-12' union all
    select 16,3,'2009-02-12' union all
    select 16,2,'2009-02-12'select * from @Public_News/*
    NewsID      CategoryID  CreateTime
    ----------- ----------- -----------------------
    1           2           2009-02-12 00:00:00.000
    2           17          2009-02-12 00:00:00.000
    3           15          2009-02-12 00:00:00.000
    4           1           2009-02-12 00:00:00.000
    5           7           2009-02-12 00:00:00.000
    6           6           2009-02-12 00:00:00.000
    7           5           2009-02-12 00:00:00.000
    8           4           2009-02-12 00:00:00.000
    9           1           2009-02-12 00:00:00.000
    10          2           2009-02-12 00:00:00.000
    11          2           2009-02-12 00:00:00.000
    12          2           2009-02-12 00:00:00.000
    13          2           2009-02-12 00:00:00.000
    14          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    16          3           2009-02-12 00:00:00.000
    16          3           2009-02-12 00:00:00.000
    16          2           2009-02-12 00:00:00.000(23 row(s) affected)
    */
    SELECT  NewsID,CategoryID,CreateTime 
    FROM @Public_News a 
    WHERE ( 
    SELECT count(*) FROM @Public_News b WHERE b.CategoryID=a.CategoryID AND 
    b.newsID>a.newsID  )<5 
    order by CategoryID,CreateTime desc/*
    NewsID      CategoryID  CreateTime
    ----------- ----------- -----------------------
    9           1           2009-02-12 00:00:00.000
    4           1           2009-02-12 00:00:00.000
    16          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    15          2           2009-02-12 00:00:00.000
    16          3           2009-02-12 00:00:00.000
    16          3           2009-02-12 00:00:00.000
    8           4           2009-02-12 00:00:00.000
    7           5           2009-02-12 00:00:00.000
    6           6           2009-02-12 00:00:00.000
    5           7           2009-02-12 00:00:00.000
    3           15          2009-02-12 00:00:00.000
    2           17          2009-02-12 00:00:00.000(17 row(s) affected)
    */