而在sql中循环前10名

时间:2012-06-27 19:41:10

标签: sql-server sql-server-2008

现在,我试图通过循环从表中获取前10个数据。

 Select ClientUserName, DestHost, count(DestHost) counts  from  #ProxyLog_record  
 where ClientUserName =(Select top 1 ClientUserName from #ProxyLog_count_2) 
 Group by ClientUserName, DestHost order by counts desc 

这只能获取来自clientusername的顶级数据如何将其循环到它将获得第一,第二,第三......第十个数据的位置?

2 个答案:

答案 0 :(得分:0)

 Group by ClientUserName, DestHost order by counts desc LIMIT 10

答案 1 :(得分:0)

你不能循环但你可以做这样的事情并改变在子查询中选择的记录数量:

   Select ClientUserName, DestHost, count(DestHost) counts  from  #ProxyLog_record  
     where ClientUserName  in (Select top 10 ClientUserName from #ProxyLog_count_2) 
     Group by ClientUserName, DestHost order by counts desc