2013-05-27 39 views
1

如何使用子查询编写计数(*)?如何从子查询中计数(*)

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

在上面的两行,上一部作品,但在第二行中,我得到的错误:

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

但不(select * from Firms) == Firms

编辑: 但是这个:

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 

我得到的错误:

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. 

我该如何解决这个问题?

+1

不仅是子句中的子查询无效订单,它不会告发”完成任何有用的事情。 –

回答

2

你缺少对第二个版本的别名:

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

SQL Server要求所有派生表和子查询的别名

+0

谢谢,那工作 – omega

+0

我试过这个对我的其他查询,但我得到一个错误。我把它发布在我上面的第一篇文章上。 – omega

+1

@omega删除'order by'它们不允许在没有'top'的子查询中使用 – Taryn