如何在sql中获得前十名的结果?

时间:2009-05-20 08:56:40

标签: sql sql-server sql-server-2005

这个delow查询给我一些错误:

declare @date1 nvarchar(100) , @date2 nvarchar(100)

select @date1='2009-04-20', @date2='2009-05-20'

select top 10 t.VisitingCount , t.Page
    from (
           select  Count(Page) as VisitingCount,Page
               from scr_SecuristLog   
               where Date between @date1 and @date2  
                   and [user] in (select USERNAME             
                                      from scr_CustomerAuthorities
                                 )  
               group by Page order by [VisitingCount] desc 
         ) t 

错误:

ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP或FOR XML。

4 个答案:

答案 0 :(得分:7)

我想你错过了逗号

select top 10 t.VisitingCount , t.Page from
t.VisitingCount

之后的

行中

答案 1 :(得分:2)

从派生表“t”中拉出订单

试试这个:

declare @date1 nvarchar(100) , @date2 nvarchar(100)

select @date1='2009-04-20', @date2='2009-05-20'

select top 10 t.VisitingCount , t.Page
    from (
           select  Count(Page) as VisitingCount,Page
               from scr_SecuristLog   
               where Date between @date1 and @date2  
                   and [user] in (select USERNAME             
                                      from scr_CustomerAuthorities
                                 )  
               group by Page
         ) t 
     order by [VisitingCount] desc 

答案 2 :(得分:1)

declare @date1 nvarchar(100) , @date2 nvarchar(100)

select @date1='2009-04-20', @date2='2009-05-20'

select t.VisitingCount, t.Page from(
select top 10 Count(Page) as VisitingCount,Page from scr_SecuristLog   
where Date between @date1 and @date2  
and [user] in(select USERNAME             
    from scr_CustomerAuthorities )  
group by Page order by [VisitingCount] desc ) t order by t.VisitingCount desc

答案 3 :(得分:0)

只需在内联视图中指定一些订单:

声明@ date1 nvarchar(100),@ date2 nvarchar(100)

选择@ date1 ='2009-04-20',@ date2 ='2009-05-20'

选择前10名t.VisitingCount,t.Page from( 选择Count(Page)作为VisitingCount,来自scr_SecuristLog的页面 其中@ date1和@ date2之间的日期
和[user] in(选择USERNAME
    来自scr_CustomerAuthorities 按顺序排序 - 你需要的列 )
按[VisitingCount] desc)t

按页面顺序分组