联合查询的总和 - 语法错误 - SQL Server

时间:2016-04-14 20:30:10

标签: sql sql-server

我正在尝试对联合查询求和,我正在

  

语法错误:')'

附近的语法不正确

代码:

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 个答案:

答案 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