2016-04-14 54 views
0

我試圖做聯合查詢和與我得到總和的聯合查詢 - 語法錯誤 - SQL服務器

Syntax error: incorrect syntax near ')'

代碼:

select 
    sum(rate) 
from 
    (select 
     sum(UnitsDelivered * Rate) as rate 
    from 
     Reimbursement_EBSUtilization 
    where 
     PRR_Id = 1 

    UNION ALL 

    select 
     sum(Reimbursement_CDSUtilization.Rate * CDSUtilization.UnitsDelivered) as rate 
    from 
     Reimbursement_CDSUtilization 
    left join 
     CDSUtilization on Reimbursement_CDSUtilization.CDSU_Id = CDSUtilization.Id 
    where 
     Reimbursement_CDSUtilization.PRR_Id = 270 
)--syntax error is showing here 

我確信我很失去了一些東西小而無法弄清楚

+1

'其中Reimbursement_CDSUtilization.PRR_Id = 270 )x'您需要爲您的子查詢使用任意名稱進行別名。 ''''後' –

+1

'嘗試'作爲t'其中t是一個別名 – Sachu

+0

哎呀我錯過了。謝謝。有用。 :) –

回答

2

您需要別名子查詢:

select sum(rate) from 
(
    select sum(UnitsDelivered*Rate) as rate 
    from Reimbursement_EBSUtilization 
    where PRR_Id =1 

    UNION ALL 

    select sum(Reimbursement_CDSUtilization.Rate * CDSUtilization.UnitsDelivered) as rate 
    from Reimbursement_CDSUtilization 
    left join CDSUtilization on Reimbursement_CDSUtilization.CDSU_Id = CDSUtilization.Id 
    where Reimbursement_CDSUtilization.PRR_Id = 270 
) as t -- <-- use alias here 
+0

謝謝。這工作:) –