KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ why sv needs ,’ here?

  • why sv needs ,’ here?

    Posted by kdb_newbie on June 4, 2023 at 12:00 am

    I have the below trades table in my q session:

     

    q)meta trades 
    c    | t f a 
    -----| ----- 
    time | p 
    sym  | s   g 
    src  | s   g
    price| f 
    size | i

     

     

    Then, why does it need each both for the sv function when x and y are joined already and then passed into sv:

     

    q)update symsrc:{` sv'(x,'y)}[sym;src] from 1#trades 
    time                          sym src price size symsrc 
    -------------------------------------------------------- 
    2023.06.04D08:00:49.248000000 ORCL L 40.67 3984 ORCL.L 
    
    q)update symsrc:{-1 .Q.s1 x,y; ` sv'(x,'y)}[sym;src] from 1#trades `ORCL`L 
    time                          sym src price size symsrc 
    -------------------------------------------------------- 
    2023.06.04D08:00:49.248000000 ORCL L 40.67 3984 ORCL.L

     

    kdb_newbie replied 8 months, 2 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • davidcrossey

    Member
    June 4, 2023 at 12:00 am

    You’ll get a type error without the each, because you need to create a scalar from each vector (i.e. each pair of symbols)

     

    q)t:([]sym:`ORCL`APPL;src:`L`R) t 
    sym src 
    -------- 
    ORCL L 
    APPL R 
    
    q) update symsrc:` sv (sym,'src) from t 
    'type [0] 
    update symsrc:` sv (sym,'src) from t ^ 
    
    q) update symsrc:` sv'(sym,'src) from t 
    sym src symsrc 
    --------------- 
    ORCL L ORCL.L 
    APPL R APPL.R

     

    Bonus tip – as shown above, you can avoid using a lambda by using parenthesis so the parser doesn’t interpret the comma as a new column statement.

  • cillianreilly

    Member
    June 4, 2023 at 12:00 am

    David has already answered the question here, but it’s worth noting that you can do this operation very succinctly using .Q.dd:

    q)update symsrc:.Q.dd'[sym;src]from trades 
    
    time                          sym src price size symsrc 
    -------------------------------------------------------- 
    2023.06.04D10:22:20.088377000 ORCL L 40.67 3984 ORCL.L

     

Log in to reply.