Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Subquery error near )

Tags:

sql

My subquery gives an error: 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)
like image 513
Wilest Avatar asked Jul 20 '12 08:07

Wilest


2 Answers

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
like image 102
Vishwanath Dalvi Avatar answered Oct 13 '22 05:10

Vishwanath Dalvi


Every select from subquery needs an alias. Just add an "X" in the end that will become the name of the table

NOT OK:

select * from (
   select * from your_table
) 

OK:

select * from (
   select * from your_table
) X
like image 20
Diego Avatar answered Oct 13 '22 07:10

Diego