SQL子查询错误附近)

时间:2012-07-20 08:53:16

标签: sql

我的子查询会出错:Msg 102, Level 15, State 1, Line 17 Incorrect syntax near ')'.

SELECT SalesArea, Branch, Volume
from
(select 
br.SalesArea as SalesArea
,br.Branch as Branch
, sum(a.Volume) as Volume
FROM dbo.vDetail a with (nolock) 
LEFT JOIN
dbo.vBranch AS br WITH (nolock) 
ON a.Branch = br.Branch
group by a.Volume, br.SalesArea, br.Branch)

3 个答案:

答案 0 :(得分:9)

你缺少子查询的别名试试这个。

SELECT SalesArea, Branch, Volume
from
(select 
br.SalesArea as SalesArea
,br.Branch as Branch
, sum(a.Volume) as Volume
FROM dbo.vDetail a with (nolock) 
LEFT JOIN
dbo.vBranch AS br WITH (nolock) 
ON a.Branch = br.Branch
group by a.Volume, br.SalesArea, br.Branch) as x

答案 1 :(得分:5)

子查询中的每个选择都需要一个别名。只需添加一个“X”,它将成为表格的名称

不行:

select * from (
   select * from your_table
) 

行:

select * from (
   select * from your_table
) X

答案 2 :(得分:1)

您需要派生表的别名

SELECT SalesArea, Branch, Volume 
from 
(select  
br.SalesArea as SalesArea 
,br.Branch as Branch 
, sum(a.Volume) as Volume 
FROM dbo.vDetail a with (nolock)  
LEFT JOIN 
dbo.vBranch AS br WITH (nolock)  
ON a.Branch = br.Branch 
group by a.Volume, br.SalesArea, br.Branch) as T