更新列以包含从另一列派生的值
updating a column to include values derived from another
我有一个情况使用我有一个 table (tbl1) 并向其中添加了一个列,该列当前全部为空。我想使用更新语句将其他 3 个列的值连接在一起来填充此列。这些其他列中的 1 列可以使用,但其他两列的值需要与另一个 table (tbl2) 连接以根据 table 示例查找单个字符关联代码下面来自 tbl2。来自 tbl2 的 CostType 和 RefType 存在于 tbl1 中,但我想将 CostCode 和 RefCode 带到 tbl1.I 的空列中也显示了我使用 Advantage SQL Architect 的尝试。虽然看不到让它工作。
update tbl1
set emptycolumn=tbl.column1+tbl2.costcode+tbl2.refcode
from tbl1 left join tbl2 on (tbl1.costtype=tbl2.costtype)
left join tbl2 on (tbl1.reftype=tbl2.reftype)
CostCode CostType RefCode RefType
A Apportioned Cost E EmployeeID
J Funding Adjustment I Invoice
G Grant G Grant Recipient
M Grant Management L LearnRefNumber
O Other Costs N CompanyName
E Staff Expenses O Other
F Staff Full Time C Authorised Claims
P Staff Part Time A Audit Adjustment
U Unit Cost
D Unit Cost Deduction
S Start Payment
C Completion Payment
我相信你的语法没问题,所以我猜这个值仍然是 NULL
。如果是这样,您可能只需要 coalesce()
:
update tbl1
set emptycolumn = (coalesce(tbl.column1, 0) + -- not sure if it starts as NULL
coalesce(tbl2.costcode, 0) +
coalesce(tbl2.refcode, 0)
)
from tbl1 left join
tbl2
on tbl1.costtype = tbl2.costtype left join
tbl2
on tbl1.reftype = tbl2.reftype;
如果您打算进行字符串连接,请使用 ''
而不是 0
。
我有一个情况使用我有一个 table (tbl1) 并向其中添加了一个列,该列当前全部为空。我想使用更新语句将其他 3 个列的值连接在一起来填充此列。这些其他列中的 1 列可以使用,但其他两列的值需要与另一个 table (tbl2) 连接以根据 table 示例查找单个字符关联代码下面来自 tbl2。来自 tbl2 的 CostType 和 RefType 存在于 tbl1 中,但我想将 CostCode 和 RefCode 带到 tbl1.I 的空列中也显示了我使用 Advantage SQL Architect 的尝试。虽然看不到让它工作。
update tbl1
set emptycolumn=tbl.column1+tbl2.costcode+tbl2.refcode
from tbl1 left join tbl2 on (tbl1.costtype=tbl2.costtype)
left join tbl2 on (tbl1.reftype=tbl2.reftype)
CostCode CostType RefCode RefType
A Apportioned Cost E EmployeeID
J Funding Adjustment I Invoice
G Grant G Grant Recipient
M Grant Management L LearnRefNumber
O Other Costs N CompanyName
E Staff Expenses O Other
F Staff Full Time C Authorised Claims
P Staff Part Time A Audit Adjustment
U Unit Cost
D Unit Cost Deduction
S Start Payment
C Completion Payment
我相信你的语法没问题,所以我猜这个值仍然是 NULL
。如果是这样,您可能只需要 coalesce()
:
update tbl1
set emptycolumn = (coalesce(tbl.column1, 0) + -- not sure if it starts as NULL
coalesce(tbl2.costcode, 0) +
coalesce(tbl2.refcode, 0)
)
from tbl1 left join
tbl2
on tbl1.costtype = tbl2.costtype left join
tbl2
on tbl1.reftype = tbl2.reftype;
如果您打算进行字符串连接,请使用 ''
而不是 0
。