【问题标题】:updating a column to include values derived from another更新一列以包含从另一列派生的值
【发布时间】:2018-04-19 08:38:33
【问题描述】:

我有一种情况,我有一个表 (tbl1),并在其中添加了一个当前全为空的列。我想使用更新语句来填充此列,其中包含连接在一起的 3 个其他列的值。这些其他列中的 1 个可以使用,但其他两个具有需要与另一个表 (tbl2) 连接的值,以根据下面的表示例从 tbl2 查找单个字符关联代码。 tbl2 中的 CostType 和 RefType 存在于 tbl1 中,但我想将 CostCode 和 RefCode 带到 tbl1 的空列中。我还展示了我使用 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     

【问题讨论】:

  • 它是如何“不工作”的?这是相当模糊的。

标签: sql advantage-database-server


【解决方案1】:

我相信你的语法没问题,所以我猜这个值仍然是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。

【讨论】:

  • 嗨,戈登。非常感谢。对不起,如果我含糊其辞。不过你是对的,这些值仍然为空,所以我现在就离开并尝试你的建议。
  • 工作愉快,谢谢戈登。但是,出于好奇,我想知道我是否可以在没有合并的情况下凑合?
  • @voot89 。 . .你可以改用case。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 2023-03-10
  • 2022-11-11
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多