2014-09-12 58 views
0
table 1 = customer | primary-key = customer.sid 
table 2 = orders | foreign-key = orders.customer_sid 

我想要sum orders.sale_amount where customer.sid = orders.customer_sid and update customer.sales_total从一个表中获取一笔钱并使用SQL服务器更新另一列中的一列

我是新来的SQL和这里是我原来的语句(不工作)

UPDATE customer 
inner JOIN orders ON (customer.sid = orders.customer_Sid) 
SET customer.sales_total = SUM(orders.sale_amount) 
GROUP BY customer.sid; 

回答

0
Update customer 
Set Total=Od.Tot 
From customer C 
    cross apply (Select SUM (O.sale_amount) Tot 
       From Orders O Where O.customer_Sid=C.sid) Od 
+0

主要错误0x80040E14,次要错误25501 >更新客户 合计设定= Od.Tot 从客户C 交叉应用(选择SUM(O.sale_amount)Tot 从订单O其中O.customer_Sid = C.sid)Od 解析查询时出错。 [令牌行号= 3,令牌行偏移量= 1,令牌出错=来自] – 2015-05-29 17:08:06

+0

@RobertStraight:您是否在某处使用此动态查询? – 2015-06-01 08:27:09

相关问题