此查询无效
update p
set p.TotalAmount = SUM(pay.Amount)
FROM ##tmp t
INNER JOIN SMILAPPLICATION..Purchases p on t.ShoppingCartPaymentID = p.ExternalPurchaseId
INNER JOIN SMILAPPLICATION..PurchaseItems pit ON pit.ExternalPurchaseItemId = t.ShoppingCartID
INNER JOIN SMILAPPLICATION..Sessions s on t.SessionId = s.SessionId
LEFT JOIN SMILAPPLICATION..SessionPricing sp on s.SessionId = sp.SessionId
INNER JOIN SMILAPPLICATION..Payments pay ON pay.PurchaseItemId = pit.PurchaseItemId
GROUP By p.PurchaseId
消息156,级别15,状态1,行10关键字附近的语法不正确 ' GROUP'
虽然这有效但
update y
set y.TotalAmount = x.Total
FROM
(
select p.PurchaseId, SUM(pay.Amount) AS Total
FROM ##tmp t
INNER JOIN SMILAPPLICATION..Purchases p on t.ShoppingCartPaymentID = p.ExternalPurchaseId
INNER JOIN SMILAPPLICATION..PurchaseItems pit ON pit.ExternalPurchaseItemId = t.ShoppingCartID
INNER JOIN SMILAPPLICATION..Sessions s on t.SessionId = s.SessionId
LEFT JOIN SMILAPPLICATION..SessionPricing sp on s.SessionId = sp.SessionId
INNER JOIN SMILAPPLICATION..Payments pay ON pay.PurchaseItemId = pit.PurchaseItemId
GROUP By p.PurchaseId
) x inner join SMILAPPLICATION..Purchases y on x.PurchaseId = y.PurchaseId
WHY吗
答案 0 :(得分:1)
我认为这与幕后的UPDATE statement is working方式有关,它不允许任何聚合功能:
1)你去基地桌。它不能有别名,因为别名 将创建一个工作表,将更新然后消失 声明结束后,什么都不做。
2)你转到WHERE子句。测试为TRUE的所有行(如果有!) 标记为子集。如果没有WHERE子句,那么整个表 被标记。此set /伪表的名称在Standard SQL中是OLD。
3)你转到SET子句并构造一个名为的set / pseudo-table 新。此表中的行是通过复制来自的值来构建的 从原始行到新行没有提到列。该 列一次全部分配。也就是说,工作单位是一排, 一次不是一列。
4)删除OLD子集并插入NEW集。
我想,从理论上讲,带SUM(...)
的{{1}}可以放在伪表中并复制数据,但我认为GROUP BY
适用于直接复制(快速)数据。
答案 1 :(得分:0)