0
select 
   row_number() over (order by BookTitle) AS Row,
   BookTitleID, 
   BookTitle,
   CallNumber,
   FullName,
   count(case Status when 'OnShelf' then 1 else null end) AS CopiesOnShelves

from
(  
select 
       Book.BookTitleID,
       BookTitles.BookTitle,
       BookTitles.CallNumber,
       Book.Status,
       FullName = LastName + ', ' + FirstName + ' ' + MiddleName

From
    Book
        left outer join 
    BookTitles
        on BookTitles.BookTitleID = Book.BookTitleID 
        left outer join
    Authors
        on Authors.AuthorID = BookTitles.AuthorID   ) sub
Where Row between 1 and 10 -- not working
Group By Callnumber, BookTitle, BookTitleID, FullName

在此示例中,我将如何使用 In between ROW 来显示第 1 行到第 10 行。

4

2 回答 2

0

您的 row_Number 函数需要在子查询中。您不能在声明它的查询的 where 子句中引用 row_number 列。不过,您可以从外部查询。只是那些奇怪的 TSQL 规则之一。我不确定你是否真的想要选择结果中的行,所以我还是把它扔在那里。

select 
   Row,
   BookTitleID, 
   BookTitle,
   CallNumber,
   FullName,
   count(case Status when 'OnShelf' then 1 else null end) AS CopiesOnShelves

from
(  
select 
       Book.BookTitleID,
       BookTitles.BookTitle,
       BookTitles.CallNumber,
       Book.Status,
       FullName = LastName + ', ' + FirstName + ' ' + MiddleName,
       row_number() over (order by BookTitle) AS Row

From
    Book
        left outer join 
    BookTitles
        on BookTitles.BookTitleID = Book.BookTitleID 
        left outer join
    Authors
        on Authors.AuthorID = BookTitles.AuthorID   ) sub
Where Row between 1 and 10 -- not working
Group By Callnumber, BookTitle, BookTitleID, FullName, Row
于 2013-09-09T03:27:27.483 回答
0
select * from(
select 
   Row,
   BookTitleID, 
   BookTitle,
   CallNumber,
   FullName,
   CopiesOnShelves
from
(  
select 
       Book.BookTitleID,
       BookTitles.BookTitle,
       BookTitles.CallNumber,
       FullName = LastName + ', ' + FirstName + ' ' + MiddleName,
       CopiesOnShelves = count(case Status when 'OnShelf' then 1 else null end),  
       Row = row_number() over (order by BookTitle)  
From
    Book
        left outer join 
    BookTitles
        on BookTitles.BookTitleID = Book.BookTitleID 
        left outer join
    Authors
        on Authors.AuthorID = BookTitles.AuthorID   

 Group By Book.BookTitleID, BookTitles.BookTitle, BookTitles.CallNumber, 
    LastName, FirstName, MiddleName
) sub
) sub2
WHERE Row between @start and @end
于 2013-09-07T20:43:38.110 回答