Nathan
Nathan

Reputation: 101

KDB/q Update table and insert

I want to put price qty and exchange_name but if the price already in the table i just want to update the quantity. if the price is not in the table I want to insert it. Is there a function to check if the price is already present, update it and if not insert it? Else how do I check if price in the table .

table:([]price:();qty:();exchange_name:())

Upvotes: 0

Views: 1007

Answers (1)

nikeros
nikeros

Reputation: 3379

Can't you key the table by price (or price/exchange if that is what you need) and upsert the fields that way? In code:

table:([] price:`float$(); qty:`int$(); exchange_name:`symbol$());

table:xkey[`price;table];

You can insert the rows as follow:

table:table upsert (20;200;`XPAR)

If you try to insert a row whose price is already shown in the table, qty and exchange_name will be updated; otherwise, a new row will be inserted.

Upvotes: 3

Related Questions