sql - SQL update value in row with value from another row
问题描述
I need to iterate through a large data set and populate the Product ID into the Special ID field for the negative BaseCurrencyValue for all Products contained that are contained inside that GUID
So in the example below the Special ID should be 1328 for all rows
I know how to do a straight update like
update mytable
set SpecialID = productID
where SpecialID is null
But I don't know how to update the other non negative Prices into it
saleitemid productid quantity BaseCurrencyPrice saleguid specialsetinstanceid specialid
393191236 34 2 10 48EC5DF9 1 NULL
393191235 88 2 10 48EC5DF9 1 NULL
393191237 1328 1 -5 48EC5DF9 1 NULL
解决方案
自加入:
update m1
set m1.SpecialID = m2.productID
from mytable m1 inner join mytable m2
on m2.saleguid = m1.saleguid and m2.basecurrencyprice < 0
where m1.SpecialID is null
请参阅演示。
结果:
> saleitemid | productid | quantity | BaseCurrencyPrice | saleguid | specialsetinstanceid | specialid
> ---------: | --------: | -------: | ----------------: | :------- | -------------------: | :--------
> 393191236 | 34 | 2 | 10 | 48EC5DF9 | 1 | 1328
> 393191235 | 88 | 2 | 10 | 48EC5DF9 | 1 | 1328
> 393191237 | 1328 | 1 | -5 | 48EC5DF9 | 1 | 1328
推荐阅读
- c# - 循环错误,包括字符串和变量
- sqlite - 完整的 SQLite 新手 - 如何为 UWP 应用安装 SQLCipher
- mxgraph - 在 draw.io 内部的 mxgraph 中,我们会弹出一个包含文本区域的对话框。选择时如何控制滚动速度?
- python - 在新环境中安装自制软件包(mopidy)时遇到问题
- javascript - jquery svg 路径:单击路径 B 检查路径 A 当前是否处于活动状态,如果是,则停用 A 并激活 B
- xamarin - 编译 Xamarin Forms 应用程序时出错
- python - 为什么此代码无法正常用于关键字搜索?
- python - 在 google colab 上导入自己的 ipynb 文件
- javascript - 如何在表单中查找第一个输入的名称,但排除名称为“__RequestVerificationToken”的输入
- ios - 无法设置 UISwitch 的 isOn 属性