基于行信息的条件更新字段

时间:2018-08-23 04:30:19

标签: kdb

在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

可以帮我吗?

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:1)

您可以使用字典来填充价格

q)d:`nut`bolt`cam!1.2 1.5 2
q)update newPrices:prices^prices*d items from tab
items sales prices newPrices
----------------------------
nut   6     10     12       
bolt  8     20     30       
cam   0     15     30       
cog   3     20     20       
bolt                        
screw    
相关问题