2013-05-27 3 views
1

하위 쿼리를 사용하여 카운트 (*)를 작성하려면 어떻게해야합니까? (select * from Firms) == Firms되지하위 쿼리에서 카운트 (*)하는 방법

Msg 102, Level 15, State 1, Line 1 
Incorrect syntax near ')'. 

그러나 : 위의 두 줄에서

select count(*) from Firms 
select count(*) from (select * from Firms) 

, 상단 하나는 작동하지만 두 번째 줄에, 나는 오류가?

편집 : 이것에 대한 그러나 :

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

가 어떻게이 문제를 해결할 수 있습니다
select count(*) from 
    (
     select HireResponseID, HireResponse, DateResponse, Comments, YearFileOpened, file_number, isCaseOpen, last_update, isConfidential, date_created, OurClient, TheirClient, ProjectName, description, lawyer_lastname, lawyer_firstname, Conflicts.ConflictID 
     from Hire_Response, Conflicts, Lawyers 
     WHERE Hire_Response.ConflictID=Conflicts.ConflictID AND Lawyers.lawyerID=Conflicts.lawyerID AND firmID = @FirmID AND HireID = @HireID AND isStillaConflict = 1 
     ORDER BY file_number, TheirClient, OurClient, lawyer_lastname, lawyer_firstname 
    ) as data 

내가 오류가? 당신은 두 번째 버전에 별칭을 누락

+1

뿐만 아니라 하위 쿼리에 잘못된 order by 절은, 그것이 같으면이다 유용한 것을 이루지 못한다. –

답변

2

는 :

select count(*) 
from 
(
    select * -- change this to the column names - you shouldn't use select * 
    from Firms 
) f -- this is missing 

SQL 서버는 모든 파생 된 테이블 및 하위 쿼리에 별칭을 필요로

+0

덕분에, – omega

+0

나는이 다른 쿼리를 시도했지만 오류가 발생합니다. 위의 첫 번째 게시물에 게시했습니다. – omega

+1

@omega는'order by '을 제거합니다. 하위 쿼리에서는 허용되지 않는'top' – Taryn

관련 문제