0

我编写了一个 SQL Server 查询来显示表中的行号和总行数。

到目前为止,它工作正常

前任:

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

现在我只需要显示 10 行,如果我试图限制每个表 10 行,那么它会抛出错误,如无效的列名行号

前任:

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

消息 207,级别 16,状态 1,第 2
行列名“行号”无效。

我如何解决它?

提前致谢

4

2 回答 2

3

partition byrow_number(). 我想你可以做你想做的事top

 select top 10 row_number() over (order by partyid) as Rownumbers,
        count(*) over() as totalrows, partyname 
 from party
 order by partyid
于 2013-07-09T03:02:12.010 回答
0

一种选择是使用子查询:

select *
from (
    select 
         row_number() over (order by partyid) as Rownumbers,
         count(*) over() totalrows,
         partyname 
    from party 
) t
where Rownumbers between 1 and 5
于 2013-07-09T02:49:20.750 回答