简体   繁体   English

如何限制10行在SQL Server 2008 R2中显示?

[英]How to restrict 10 rows to dispaly in SQL Server 2008 R2?

I wrote a SQL Server query to display rownumbers and total rows to the table . 我编写了一个SQL Server查询,以显示表的行号和总行数。

Up to this it is working fine 到目前为止,它工作正常

ex: 例如:

select 
    row_number() over (order by  partyid) as Rownumbers,
    count(*) over() totalrows,
    partyname 
from party

Now I need to display only 10 rows, if I am trying to restrict 10 rows per table then it is throwing the error like invalid column name rownumbers 现在我只需要显示10行,如果我试图限制每个表10行,那么它将抛出错误,例如无效的列名rownumbers

ex: 例如:

select 
   row_number() over (order by  partyid) as Rownumbers,
   count(*) over() totalrows,
   partyname 
from party
where Rownumbers between 1 and 5

Msg 207, Level 16, State 1, Line 2 消息207,第16级,状态1,第2行
Invalid column name 'Rownumbers'. 无效的列名“行号”。

How do I fix it? 我如何解决它?

Thanks in advance 提前致谢

You don't have a partition by clause in the row_number() . 您在row_number()没有partition by子句。 I think yo can do what you want using top : 我认为您可以使用top做您想做的事情:

 select top 10 row_number() over (order by partyid) as Rownumbers,
        count(*) over() as totalrows, partyname 
 from party
 order by partyid

One option is to use a subquery: 一种选择是使用子查询:

select *
from (
    select 
         row_number() over (order by partyid) as Rownumbers,
         count(*) over() totalrows,
         partyname 
    from party 
) t
where Rownumbers between 1 and 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM