我有代码根据某些逻辑选择价格。我们的选择是目标保证金,平均历史价格和市场价格(这些都已计算完毕)。有时候,我们想忽略这种逻辑并明确地说'#34;对于这个模型,请使用这个价格,而不是它已经选择的价格"。但我也持有我用过的价格。所以这对我的价格没有任何问题:
Update A
Set [Price] = Case
When [Logic Override] = 1 THEN [Target Margin Price]
When [Logic Override] = 2 THEN [Average Historical Price]
When [Logic Override] = 3 THEN [Market Price]
Else [Price]
END
From PriceTable A
Where ([Logic Override] <> 4 and [Logic Override] is not null)
and [Model_ID] = Coalesce(NullIf(@ModelID, ''), [Model_ID])
我希望能够做的还是设置[定价方法]。如果逻辑覆盖是1,我必须创建一个全新的案例陈述,当我真正想要做的是将价格和定价方法分别设置为20美元和“目标保证金”时。我似乎无法获得正确的语法来执行此操作。
答案 0 :(得分:0)
您可以在所有列中创建多个sql逻辑(只要您尝试更新同一个表)。
您期望的结果令人困惑,并且不确定您实际需要什么,但我希望这有助于您了解使用每个不同的WHERE子句更新多个列。我认为你试图问。
Update A
Set [Price] = (Select Case
When [Logic Override] = 1 THEN [Target Margin Price]
When [Logic Override] = 2 THEN [Average Historical Price]
When [Logic Override] = 3 THEN [Market Price]
Else [Price]
END
From PriceTable A
Where ([Logic Override] <> 4 and [Logic Override] is not null)
and [Model_ID] = Coalesce(NullIf(@ModelID, ''), [Model_ID]),
[Pricing Method] = (Select '$20' as [Pricing Method] from PriceTable A where [logic
Override] = 1),
[Price] = (Select '$20' as [Price] from PriceTable A where [logic Override] = 1),
[Target Margin] = (Select '$20' as [Target Margin] from PriceTable A where [logic Override] = 1)
From PriceTable
我实际上并不推荐这些类型的更新,因为它们很难维护(如果有多个逻辑,表等需要更新,这很麻烦)。我建议您为每个列创建一个更新,以便您的同行更容易维护,阅读和理解。但对他自己来说。
答案 1 :(得分:0)
我尝试将它们作为两个单独的案例陈述连续编写,只有第一个更新了价格。第二个根本没有更新。所以它选择了合适的价格,但不是正确的定价方法。这是实际工作的。
Update A
Set [Price] = Case
When [Logic Override] = 1 THEN [Target Margin Price]
When [Logic Override] = 2 THEN [Average Historical Price]
When [Logic Override] = 3 THEN [Market Price]
Else [FRP]
END,
[Pricing Method] = Case
When [Logic Override] = 1 THEN 'Target Margin'
When [Logic Override] = 2 THEN 'Historical Average'
When [Logic Override] = 3 THEN 'Market Price'
Else [FRP]
END
From PriceTable A
Where ([Logic Override] <> 0 and [Logic Override] is not null)
and [Model_ID] = Coalesce(NullIf(@ModelID, ''), [Model_ID])
我真的不确定为什么我无法使用两个单独的更新语句,但由于这有效,我并不太担心。它的代码比我想要的多,但我不太担心,因为它有效。