2012-07-20 49 views
4

我的子查詢提供了一個錯誤:Msg 102, Level 15, State 1, Line 17 Incorrect syntax near ')'.SQL子查詢錯誤附近)

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) 
+6

你可能需要給子查詢的別名。 )qry。 – 2012-07-20 08:54:42

+0

謝謝!解決了它。 – Wilest 2012-07-20 08:56:04

回答

5

你缺少別名子查詢嘗試這個。

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

您需要別名爲派生表

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 
5

從子查詢每一個選擇需要的別名。只需添加在最後的 「X」,這將成爲該表

NOT OK的名字:

select * from (
    select * from your_table 
) 

OK:

select * from (
    select * from your_table 
) X 
+0

什麼.....:O – nobalG 2017-07-18 09:00:59