[步骤]
UPDATE RB_ProductSET [UpdateTime] = Getdate(),
LastSupplierPrice = 15.0000
WHERE SKU = '9071700'
AND CustomerID = 1; --修改组合产品供货价
UPDATE RB_Product
SET UpdateTime = Getdate(),
RB_Product.LastSupplierPrice = (SELECT Sum(p.LastSupplierPrice * g.SkuNum)
FROM RB_Product p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
WHERE g.GroupSku = RB_Product.SKU
AND g.CustomerID = 1)
FROM RB_Product
WHERE SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '9071700'
AND CustomerID = 1)
AND CustomerID = 1 --修改组合产品供货价,产品价,打包费
UPDATE a
SET a.UpdateTime = Getdate(),
a.SupplierPrice = (SELECT Sum(Isnull(p.SupplierPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.SupplierProductPrice = (SELECT Sum(Isnull(p.SupplierProductPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.PackingFee = (SELECT Sum(Isnull(p.PackingFee, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku)
FROM RB_Product_SupplierPrice a
WHERE a.IsDefault = 1
AND a.SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH (NOLOCK)
WHERE SKU = '9071700'
AND CustomerID = 1)
AND a.CustomerID = 1
UPDATE RB_Product
SET [UpdateTime] = Getdate(),
LastSupplierPrice = 14.0000
WHERE SKU = '8297067'
AND CustomerID = 1; --修改组合产品供货价
UPDATE RB_Product
SET UpdateTime = Getdate(),
RB_Product.LastSupplierPrice = (SELECT Sum(p.LastSupplierPrice * g.SkuNum)
FROM RB_Product p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
WHERE g.GroupSku = RB_Product.SKU
AND g.CustomerID = 1)
FROM RB_Product
WHERE SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '8297067'
AND CustomerID = 1)
AND CustomerID = 1 --修改组合产品供货价,产品价,打包费
UPDATE a
SET a.UpdateTime = Getdate(),
a.SupplierPrice = (SELECT Sum(Isnull(p.SupplierPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.SupplierProductPrice = (SELECT Sum(Isnull(p.SupplierProductPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.PackingFee = (SELECT Sum(Isnull(p.PackingFee, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku)
FROM RB_Product_SupplierPrice a
WHERE a.IsDefault = 1
AND a.SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH (NOLOCK)
WHERE SKU = '8297067'
AND CustomerID = 1)
AND a.CustomerID = 1
UPDATE RB_Product
SET [UpdateTime] = Getdate(),
LastSupplierPrice = 4.3000
WHERE SKU = '6600740'
AND CustomerID = 1; --修改组合产品供货价
UPDATE RB_Product
SET UpdateTime = Getdate(),
RB_Product.LastSupplierPrice = (SELECT Sum(p.LastSupplierPrice * g.SkuNum)
FROM RB_Product p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
WHERE g.GroupSku = RB_Product.SKU
AND g.CustomerID = 1)
FROM RB_Product
WHERE SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '6600740'
AND CustomerID = 1)
AND CustomerID = 1 --修改组合产品供货价,产品价,打包费
UPDATE a
SET a.UpdateTime = Getdate(),
a.SupplierPrice = (SELECT Sum(Isnull(p.SupplierPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.SupplierProductPrice = (SELECT Sum(Isnull(p.SupplierProductPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.PackingFee = (SELECT Sum(Isnull(p.PackingFee, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku)
FROM RB_Product_SupplierPrice a
WHERE a.IsDefault = 1
AND a.SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH (NOLOCK)
WHERE SKU = '6600740'
AND CustomerID = 1)
AND a.CustomerID = 1
UPDATE RB_Product
SET [UpdateTime] = Getdate(),
LastSupplierPrice = 14.4000
WHERE SKU = '17693953'
AND CustomerID = 1; --修改组合产品供货价
UPDATE RB_Product
SET UpdateTime = Getdate(),
RB_Product.LastSupplierPrice = (SELECT Sum(p.LastSupplierPrice * g.SkuNum)
FROM RB_Product p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
WHERE g.GroupSku = RB_Product.SKU
AND g.CustomerID = 1)
FROM RB_Product
WHERE SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '17693953'
AND CustomerID = 1)
AND CustomerID = 1 --修改组合产品供货价,产品价,打包费
UPDATE a
SET a.UpdateTime = Getdate(),
a.SupplierPrice = (SELECT Sum(Isnull(p.SupplierPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.SupplierProductPrice = (SELECT Sum(Isnull(p.SupplierProductPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.PackingFee = (SELECT Sum(Isnull(p.PackingFee, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku)
FROM RB_Product_SupplierPrice a
WHERE a.IsDefault = 1
AND a.SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH (NOLOCK)
WHERE SKU = '17693953'
AND CustomerID = 1)
AND a.CustomerID = 1
UPDATE RB_Product
SET [UpdateTime] = Getdate(),
LastSupplierPrice = 28.0000
WHERE SKU = '14945233'
AND CustomerID = 1; --修改组合产品供货价
UPDATE RB_Product
SET UpdateTime = Getdate(),
RB_Product.LastSupplierPrice = (SELECT Sum(p.LastSupplierPrice * g.SkuNum)
FROM RB_Product p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
WHERE g.GroupSku = RB_Product.SKU
AND g.CustomerID = 1)
FROM RB_Product
WHERE SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '14945233'
AND CustomerID = 1)
AND CustomerID = 1 --修改组合产品供货价,产品价,打包费
UPDATE a
SET a.UpdateTime = Getdate(),
a.SupplierPrice = (SELECT Sum(Isnull(p.SupplierPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.SupplierProductPrice = (SELECT Sum(Isnull(p.SupplierProductPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.PackingFee = (SELECT Sum(Isnull(p.PackingFee, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku)
FROM RB_Product_SupplierPrice a
WHERE a.IsDefault = 1
AND a.SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH (NOLOCK)
WHERE SKU = '14945233'
AND CustomerID = 1)
AND a.CustomerID = 1
UPDATE RB_Product
SET [UpdateTime] = Getdate(),
LastSupplierPrice = 5.8900
WHERE SKU = '12029613'
AND CustomerID = 1; --修改组合产品供货价
UPDATE RB_Product
SET UpdateTime = Getdate(),
RB_Product.LastSupplierPrice = (SELECT Sum(p.LastSupplierPrice * g.SkuNum)
FROM RB_Product p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
WHERE g.GroupSku = RB_Product.SKU
AND g.CustomerID = 1)
FROM RB_Product
WHERE SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '12029613'
AND CustomerID = 1)
AND CustomerID = 1 --修改组合产品供货价,产品价,打包费
UPDATE a
SET a.UpdateTime = Getdate(),
a.SupplierPrice = (SELECT Sum(Isnull(p.SupplierPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.SupplierProductPrice = (SELECT Sum(Isnull(p.SupplierProductPrice, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku),
a.PackingFee = (SELECT Sum(Isnull(p.PackingFee, 0) * g.SkuNum)
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = a.sku)
FROM RB_Product_SupplierPrice a
WHERE a.IsDefault = 1
AND a.SKU IN (SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH (NOLOCK)
WHERE SKU = '12029613'
AND CustomerID = 1)
AND a.CustomerID = 1
[结果]
[期望]
语句过长 SQL 简析慢,建议拆分 优先把值算出来 然后再处理
SELECT DISTINCT GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '9071700'
AND CustomerID = 1
改语句 优先 算出 SKU 然后在带入 其他SQL 运算
而且 该SQL 应该 改成
SELECT top 1 GroupSku
FROM RB_Product_GroupSku WITH ( NOLOCK )
WHERE SKU = '9071700'
AND CustomerID = 1
优先 算出 值

同一个 SKU 不存在 与多个 组合SKU 中
下个子查询 也优先算出来 SupplierPrice SupplierProductPrice PackingFee
SELECT Sum(Isnull(p.SupplierPrice, 0) * g.SkuNum) SupplierPrice, Sum(Isnull(p.SupplierProductPrice, 0) * g.SkuNum) SupplierProductPrice,Sum(Isnull(p.PackingFee, 0) * g.SkuNum) PackingFee
FROM RB_Product_SupplierPrice p WITH (NOLOCK)
INNER JOIN RB_Product_GroupSku g WITH (NOLOCK)
ON p.SKU = g.SKU
AND p.CustomerID = g.CustomerID
AND p.IsDefault = 1
WHERE g.CustomerID = 1
AND g.GroupSku = '9071700'
然后带入 需要处理的SQL 。这样 后面 更新的 SQL 语句 就变得简单易执行