简体   繁体   English

SQL子查询错误附近)

[英]SQL Subquery error near )

My subquery gives an error: Msg 102, Level 15, State 1, Line 17 Incorrect syntax near ')'. 我的子查询给出错误: 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)

You are missing alias for subquery try out this. 你缺少子查询的别名试试这个。

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

Every select from subquery needs an alias. 子查询中的每个选择都需要别名。 Just add an "X" in the end that will become the name of the table 只需在最后添加一个“X”,它将成为表的名称

NOT OK: 不好:

select * from (
   select * from your_table
) 

OK: 好:

select * from (
   select * from your_table
) X

You need alias name for a derived table 您需要派生表的别名

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM