create table A_test( [id] int   IDENTITY(1,1) ,CategoriesID int, Title  varchar(20),content varchar(10))
---------------------------------
insert into A_test
select 
   1          ,   'xx1',   'dd' union
select    1         ,    'xx2',  'dd' union
select    1        ,     'xx3' ,  'dd' union
select   2       ,     'xx4',   'dd' union
 select   2        ,     'xx5' , 'dd' union
 select   2         ,    'xx6'  , 'dd' union
 select  2         ,    'xx8' ,  'dd'union
select   2       ,     'xx9',   'dd' union
 select   2        ,     'xx10' , 'dd' union
 select   2         ,    'xx11'  , 'dd' union
 select  2         ,    'xx17' ,  'dd' union 
 select  2         ,    'xx27' ,  'dd'  
----------------------------------------------------
insert into A_test
select 
   1          ,   'xx1',   'dd' union
select    1         ,    'xx2',  'dd' union
select    1        ,     'xx3' ,  'dd' union
select   2       ,     'xx4',   'dd' union
 select   2        ,     'xx5' , 'dd' union
 select   2         ,    'xx6'  , 'dd' union
 select  2         ,    'xx8' ,  'dd'union
select   2       ,     'xx9',   'dd' union
 select   2        ,     'xx10' , 'dd' union
 select   2         ,    'xx11'  , 'dd' union
 select  2         ,    'xx17' ,  'dd' union 
 select  2         ,    'xx27' ,  'dd'  -----------------------------------------------
select * ,(select sum(1)from a_test (nolock) where CategoriesID=a.CategoriesID and [id]<=a.[id] ) as Tid 
into #tmp 
from a_test a(nolock) 
select [id],categoriesid,title,content  ,tid
from #tmp where tid<=10
order by categoriesid,tid
--drop table #tmp  delete  from atest---------test ------------
id          categoriesid title                content    tid         
----------- ------------ -------------------- ---------- ----------- 
1           1            xx1                  dd         1
2           1            xx2                  dd         2
3           1            xx3                  dd         3
8           1            xx1                  dd         4
9           1            xx2                  dd         5
10          1            xx3                  dd         6
20          1            xx1                  dd         7
21          1            xx2                  dd         8
22          1            xx3                  dd         9
32          1            xx1                  dd         10
4           2            xx4                  dd         1
5           2            xx5                  dd         2
6           2            xx6                  dd         3
7           2            xx7                  dd         4
11          2            xx10                 dd         5
12          2            xx11                 dd         6
13          2            xx17                 dd         7
14          2            xx27                 dd         8
15          2            xx4                  dd         9
16          2            xx5                  dd         10(影響 20 個資料列)