如何获得sql中的前十名结果?
此delow查询给了我一些错误:如何获得sql中的前十名结果?,sql,sql-server,sql-server-2005,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_Secu
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
错误:
除非还指定了TOP或FOR XML,否则ORDER BY子句在视图、内联函数、派生表、子查询和公共表表达式中无效。我认为您遗漏了逗号
select top 10 t.VisitingCount , t.Page from
在t.VisitingCount之后的上一行中,只需在内联视图中指定一些顺序: 声明@date1 nvarchar100,@date2 nvarchar100 选择@date1='2009-04-20',@date2='2009-05-20' 从中选择前10名t.访问次数,t.页面 选择CountPage作为访问计数,从scr\U SecuristLog中选择页面 其中日期介于@date1和@date2之间 和[用户]选择用户名 来自scr_客户权限 按您需要的列排序
按页面分组按[VisitingCount]desc t排序从派生表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
考虑到查询结构和子查询的使用,这里似乎有些问题。可能会建议您提供相关的表格结构,以帮助提供解决方案。我改变我的问题…我真的不理解herenvarchar100在约会中使用ORDER?!使用DATETIME或SMALLDATETIME有什么问题?来吧。。。不允许在子查询中使用ORDER BY,请在发布问题之前阅读错误消息:Prashant我需要ORDER BY。我如何使用它?
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