Category Archives: KDB

KDB – Converting a non-keyed table into a keyed table

Turning a non-keyed table into a keyed table

Define a non-keyed table from the Q command prompt as follows:

q) FxPairTable : ([] sym:`symbol$(); date:`date$(); val:`float$())

Next insert illustrative data:

insert [`FxPairTable] (`EURUSD; 2012.02.10; 1.2874)
insert [`FxPairTable] (`EURUSD; 2012.02.10; 1.2874)
insert [`FxPairTable] (`EURUSD; 2012.02.11; 1.2901)

In practice, I prefer to put the data load commends within a Q file to be sourced. This makes for easier development, and a more repeatable process.

We can list the table contents by typeing the table name at the command prompt:

q) FxPairTable

My table was listed as follows:

sym date val
------------------------
EURUSD 2012.02.10 1.2874
EURUSD 2012.02.10 1.2874
EURUSD 2012.02.11 1.2901

How do we convert this table into a keyed table?

`sym`date xkey `FxPairTable

Now, again we list the table by typing the table name at the command prompt:

q) FxPairTable

which displays the following table:


sym date | val
-----------------| ------
EURUSD 2012.02.10| 1.2874
EURUSD 2012.02.10| 1.2874
EURUSD 2012.02.11| 1.2901