Riley Hun
Riley Hun

Reputation: 2785

KDB+ Apply Conditional and then Update Multiple Columns in Table

I have a table with the following column names: portfolio, tradeType, currency, security, tradeQuantity, tradeMV, totalTradeMV, transferMV, and adjustedMV

I want to reduce the tradeQuantity, tradeMV, totalTradeMV, transferMV columns by the adjustedMV

Here is an example query:

odTradesAdjusted:update tradeQuantity:tradeQuantity-0f^adjustedMV,tradeMV:tradeMV-0f^adjustedMV,totalTradeMV:totalTradeMV-0f^adjustedMV,transferMV:transferMV-0f^adjustedMV from odTradesAdjusted

However, I want to do this only if the adjustedMV is less than the other columns. I could do a vector conditional using ? for each of the columns, but is there a more elegant way of doing this rather than writing out the vector conditional 4 times?

Upvotes: 1

Views: 772

Answers (1)

terrylynch
terrylynch

Reputation: 13657

You can use apply (@) with a little bit of subtraction logic:

@[t;`tradeQuantity`tradeMV`totalTradeMV`transferMV;{x-y*0<x-y}[;t`adjustedMV]]

Edit - this can be simplified to

@[t;`tradeQuantity`tradeMV`totalTradeMV`transferMV;{x-y*y<x}[;t`adjustedMV]]

Upvotes: 3

Related Questions