KX Community

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

Home Forums kdb+ KDB table with an array as element

  • KDB table with an array as element

    Posted by renbright on August 19, 2022 at 12:00 am

    I want to create a KDB table with an array:

    – for a regular one, I would have t:([]time:`time$();price:`float$()); for each time, there is only price

    – I want to have a table with a price array for each time. should I create it using t:([]time:`time$();price:())  or t:([]time:`time$();price:(()))

    renbright replied 9 months, 1 week ago 3 Members · 2 Replies
  • 2 Replies
  • darrenwsun

    Member
    August 19, 2022 at 12:00 am

    Both works, as (()) is interpreted the same as (). For this reason the first form is preferred. Note that the data type of price is determined by the data type of cell in the first row.

     

    q)t:([] time:`time$(); price:()) 
    q)meta t 
    c    | t f a 
    -----| ----- 
    time | t 
    price| 
    
    q)`t upsert (.z.t; 10 11f) `t 
    q)t 
    time price 
    ------------------ 
    21:42:07.028 10 11 
    
    q)meta t 
    c    | t f a 
    -----| ----- 
    time | t 
    price| F 
    
    q)`t upsert (.z.t; 10 11) `t 
    q)t 
    time price 
    ------------------ 
    21:42:07.028 10 11 
    21:44:30.724 10 11 
    
    q)meta t 
    c    | t f a 
    -----| ----- 
    time | t 
    price| F

     

  • gdickson

    Member
    August 19, 2022 at 12:00 am

    To define any column in a table as a list or an array, you can simply specify the column type with an empty set of parentheses, (), on definition. Following this definition, you can then upsert the price array along with the time value, and the column value for that table will be assigned to a float list type (upper-case F in meta command indicates the column is a list of floats, as opposed to a float atom which would be identified with a lower case f):

    q)t:([]time:`time$();price:()) 
    q)`t upsert (.z.t;60.57 60.61) `t 
    q)t 
    time price 
    ------------------------ 
    21:47:06.298 60.57 60.61 
    
    q)meta t 
    c    | t f a 
    -----| ----- 
    time | t 
    price| F

     

Log in to reply.