0

I have a table like:

q)tbl[`XXX]
1977 1987 1997

and I want to update the nth element from the list of years, so the above becomes

q)tbl[`XXX]
1997 1987 2007

And need it to be inplace? Been looking into the docs but having hard figuring it out.

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Alim Hasanov
  • 197
  • 1
  • 3
  • 16

2 Answers2

4

You can think of the table as a flipped dictionary. To do a dictionary update, you index in on key first:

  q)tbl:([]XXX:1977 1987 1997)
  q)dict:flip tbl
  q)dict[`XXX;n]:2007
  q)dict
  XXX| 1977 1987 2007

Therefore to do an inplace update on a table the following syntax is used:

  q)tbl:([]XXX:1977 1987 1997)
  q)tbl[`XXX]
  1977 1987 1997
  q)tbl[n;`XXX]:2007
  q)tbl[`XXX]
  1977 1987 2007
jomahony
  • 1,662
  • 6
  • 9
  • I have a follow up question: As it is now, the update is saved in memory. However, if I want the change to persist after restarting kdb, what is there to be done? As much as I understand we need to be able to save the changes to the disk! – Alim Hasanov Jul 18 '17 at 17:19
  • There are a number of methods in which the table can be saved down. – jomahony Aug 21 '17 at 15:16
  • The simplest is as a binary flat file ``save `:tbl`` where :tbl is the relative or absolute file path on disk. Splayed, where each of the columns will be saved as a binary file ```:tbl/ set .Q.en[`:.;tbl]``. .Q.en takes care of the enumeration. Finally you can further fragment by creating a partitioned table and using .Q.dpft - ``.Q.dpft[:hdbdir;.z.D;sym;tbl]`` code.kx.com/q/ref/filewords/#save code.kx.com/q/cookbook/splayed-tables code.kx.com/q/ref/dotq/#qdpft-save-table code.kx.com/q/ref/dotq/#qen-enumerate-varchar-cols – jomahony Aug 21 '17 at 16:17
2

qsql version of Jamies answer:

q)update XXX:2007 from `tbl where i=n

This will also allows you to use where clause based on other columns in the table

emc211
  • 1,369
  • 7
  • 14