2014-09-27 3 views
0

나는 순서가 필요한 쿼리를 가지고 있으며이 쿼리에서 특정 행을 선택해야합니다.쿼리에서 순서를 지정할 수 없습니다.

오류 : 내가 뭘하려고 오전입니다

Additional information: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

다음

"SELECT * FROM (SELECT" + 
         " Websites.Id as websiteId, " + 
         " Websites.Title, " + 
         " Websites.Description, " + 
         " Websites.Url, " + 
         " Websites.BannerURL, " + 
         " (Select Count(*) From Votes where WebsiteID = Websites.Id And [Unique] = 1 And Date = '" + 
         Date + "') as TotalVotes, " + 
         " ISNULL((Select AVG(rating) From WebsiteRating where WebsiteID = Websites.Id), 5) as Rating, " + 
         " Users.Username, " + 
         " (Select Count(*) From Redirects where WebsiteID = Websites.Id And [Unique] = 1 And Date = '" + 
         Date + "') as Redirects, " + 
         " RowNum = ROW_NUMBER() OVER (ORDER BY Websites.ID) " + 
         " FROM Websites " + 
         " INNER JOIN Users ON Websites.UserID = Users.Id " + 
         " Where Websites.Enabled = 1" + 
         " GROUP BY Websites.Title, Websites.Description, Websites.Url, Websites.BannerURL , Users.Username, Websites.Id" + 
         // Error 
         " ORDER BY Websites.Id DESC" + 
         ") as Table1 " + 
         "WHERE RowNum > " + number + " And RowNum <= " + amount + ""; 

을 나는 이후로 순서를 수행 할 때 :

다음
"WHERE RowNum > " + number + " And RowNum <= " + amount + ""; 

그 첫 번째 선택을 0에서 25까지의 행을 입력 한 다음 주문하십시오. 하지만 먼저 주문한 다음 해당 목록에서 25 행을 선택하고 싶습니다.

나는 여전히 Linq와 함께 작업 한 SQL 초보자입니다. 하지만 이것은 일반 SQL과 함께 작동하는 오래된 프로젝트입니다.

답변

1

이 시도하지 : 이미

"SELECT * FROM (SELECT" + 
    " Websites.Id as websiteId, " + 
    " Websites.Title, " + 
    " Websites.Description, " + 
    " Websites.Url, " + 
    " Websites.BannerURL, " + 
    " (Select Count(*) From Votes where WebsiteID = Websites.Id And [Unique] = 1 And Date = '" + 
    Date + "') as TotalVotes, " + 
    " ISNULL((Select AVG(rating) From WebsiteRating where WebsiteID = Websites.Id), 5) as Rating, " + 
    " Users.Username, " + 
    " (Select Count(*) From Redirects where WebsiteID = Websites.Id And [Unique] = 1 And Date = '" + 
    Date + "') as Redirects, " + 
    " RowNum = ROW_NUMBER() OVER (ORDER BY Websites.ID) " + 
    " FROM Websites " + 
    " INNER JOIN Users ON Websites.UserID = Users.Id " + 
    " Where Websites.Enabled = 1" + 
    " GROUP BY Websites.Title, Websites.Description, Websites.Url, Websites.BannerURL , Users.Username, Websites.Id" + 
    ") as Table1 " + 
    " WHERE RowNum > " + number + " And RowNum <= " + amount + "" + 
    " ORDER BY RowNum DESC" 
    ; 

당신을 RowNum을 통해 준비된 데이터를 정렬 했으므로 하위 쿼리에서 TOP 또는 ORDER가 필요하지 않으므로 선택한 행에 순서를 적용하면됩니다. 마지막 where 절 다음에 발생합니다.

+0

감사합니다. 하위 쿼리로 주문하고 싶다면 : TotalVotes? – Jamie

+0

소개 한 where 절 위치를 사용하여 하위 쿼리가 완료된 후 모든 열을 기준으로 정렬 할 수 있습니다. 그러나 행 선택은 특정 순서에 따라 결정되므로 표시 순서를 변경하면 RowNum도 계산되는 방식을 변경해야합니다. –

0

ORDER BY Websites.Id DESC. .

RowNum = ROW_NUMBER() OVER (ORDER BY Websites.ID Desc) 

또는 서브 쿼리 내부 사용이을의 내림차순이에 의해 & 사용 순서는 (어떤이에 대한 선 위에 주석)

SELECT top 100 percent 
관련 문제