Search code examples
kdbk

Get additional column using functional select


How to get an additional column of type string using ??

I tried this:

t:([]c1:`a`b`c;c2:1 2 3)
?[t;();0b;`c1`c2`c3!(`c1;`c2;10)]           / ok
?[t;();0b;`c1`c2`c3!(`c1;`c2;enlist(`abc))] / ok
?[t;();0b;`c1`c2`c3!(`c1;`c2;"10")]         / 'length
?[t;();0b;`c1`c2`c3!(`c1;`c2;enlist("10"))] / 'length

but got 'length error.


Solution

  • Your first case works because an atom will automatically expand to the required length. For a compound column you'll need to explicitly generate the correct length as follows

    q)select c1,c2,c3:`abc,c4:10,c5:count[i]#enlist"abc" from t
    c1 c2 c3  c4 c5
    ------------------
    a  1  abc 10 "abc"
    b  2  abc 10 "abc"
    c  3  abc 10 "abc"
    
    // in functional form
    q)?[t;();0b;`c1`c2`c3!(`c1;`c2;(#;(count;`i);(enlist;"abc")))]
    c1 c2 c3
    -----------
    a  1  "abc"
    b  2  "abc"
    c  3  "abc"
    

    Jason