delita
delita

Reputation: 1581

Conditional update field based on row information

In KDB I have this table:

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

I would like to compute a 4th column based on a condition, for instance:

 if[adjust; prices * factor;]  

The aim is to get the following result:

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

Can some please help me out?

Upvotes: 0

Views: 247

Answers (2)

EllenM92
EllenM92

Reputation: 51

I think you're looking for something like:

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

Upvotes: 5

Abdulrahman
Abdulrahman

Reputation: 61

You can use a dictionary and fill the prices

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    

Upvotes: 1

Related Questions