Terry
Terry

Reputation: 519

Insert a record that has many null values in kdb

Assume I only want to insert for first column and other columns to be null. What's the fastest way? and there may be different types.

Assume 10 columns. This is what I can think of:

`tab insert (`abcd;`;"";`;"";"";`;`;`;`;`;`)???

Any faster way?

Upvotes: 3

Views: 1566

Answers (3)

Sean O'Hagan
Sean O'Hagan

Reputation: 1697

The trick you are looking for is "first 0#" - this used on any datatype will create the null for that datatype, allowing you to dynamically insert nulls alongside your actual data insert

//create typed tab(schema) with all possible datatypes except c
tab:flip (`$'c)!(c:.Q.t except " ")$\:()

//(first 0#) each datatype to gather nulls for every column
//modify first column as you want to insert real data there
//insert results to tab
`tab insert @[(first 0#) each flip tab;first cols tab;:;1b]

//if you want to insert two actual events, say for col m and col t
`tab insert @[(first 0#) each flip tab;`m`t;:;(2017.01m;10:00t)]

http://code.kx.com/q/ref/dotq/#qff-append-columns may also prove useful further down the line if you require additional columns

HTH,
Sean

Upvotes: 2

jeonw
jeonw

Reputation: 133

If you have initial table's schema defined properly, i believe you don't need to provide null/default value for the columns that you are not trying to update.

For instance,

    t:flip `date`sym`ts`qty!(`d1`d1`d1`d1`d1`d1`d2;`s1`s1`s2`s1`s1`s2`s1;`t1`t1`t2`t3`t4`t5`t1;-100 -100 200 200 500 -300 -400)
    `t insert (`date`qty)!(`d10; -88f)

above will only add date and qty value and when you check the type of the table via

    meta t

you can see that the types of the table doesn't get changed

Upvotes: 0

Connor Gervin
Connor Gervin

Reputation: 946

Try specifying the values (2nd Parameter) to insert in dictionary format:

q)tab:([]a:1 2 3;b:3 4 5;c:4 5 6;d:5 6 7;e:6 7 8)
q)`tab insert `a`b!1 2
q)tab
a b c d e
---------
1 3 4 5 6
2 4 5 6 7
3 5 6 7 8
1 2

Edit:

/1 record/1 column
q)dict:enlist[`column1]!enlist[`abcd]
q)`tab insert dict

/N records/1 column
q)dict:enlist[`column1]!enlist(`abcd`efgh`ijkl)
q)`tab insert flip dict

Upvotes: 2

Related Questions