使用SQL Server Compact Edition(2008 R2)
Customer
和Orders
)SID
,订单:Customer_SID
)我想对Orders.Sales_Amount
求和,并根据Customer.Sales_Total
将总数写为SID
。
我必须错误地使用inner join
语句,因为我在FROM
语句中收到错误。
UPDATE customer
SET sales_total = aggr.sales_total
FROM customer
INNER JOIN (
SELECT sid
,sum(sales_amount) sales_total
FROM customer
INNER JOIN orders
ON (customer.sid = orders.customer_Sid)
GROUP BY customer.sid
) aggr
ON customer.sid = aggr.sid;
答案 0 :(得分:1)
有一种更简单的方法来完成您所追求的更新:
UPDATE customer
SET sales_total = (SELECT SUM(sales_amount)
FROM orders
WHERE orders.customer_Sid = customer.sid)