首页 > 解决方案 > 基于行信息的条件更新字段

问题描述

在 KDB 我有这张表:

q)tab

items sales prices adjust factor
--------------------------------
nut   6     10     1b     1.2
bolt  8     20     1b     1.5
cam   0     15     1b     2
cog   3     20     0b     0n
nut   6     10     0b     0n
bolt  8     20     0b     0n

我想根据条件计算第四列,例如:

 if[adjust; prices * factor;]  

目的是得到以下结果:

items sales prices newPrices
----------------------------
nut   6     10     12
bolt  8     20     30
cam   0     15     30 
cog   3     20     20
nut   6     10     10
bolt  8     20     20

有人可以帮我吗?

标签: kdb

解决方案


我认为您正在寻找类似的东西:

q) update newPrices:?[adjust;prices*factor;prices] from tab
items sales prices adjust factor newPrices
------------------------------------------
nut   6     10     1      1.2    12
bolt  8     20     1      1.5    30
cam   0     15     1      1      15
cog   3     20     0             20
nut   6     10     0             10
bolt  8     20     0             20

推荐阅读