KX Community

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

Home Forums kdb+ Insert a not matching dictionary to a table

  • Insert a not matching dictionary to a table

    Posted by jhug on April 22, 2022 at 12:00 am

    Hello,

    I would like to append a row to a table with data that is saved as a dictionary. The dictionary might have additional keys when comparing it to the columns in the table so upsert is not possible. I would like to only upsert the values from the dictionary that have corresponding columns in the dictionary. How is this archived?

     

    Sample Code

    Dictionary td2 and table x where values for  `a and `b should be appended as rows in table x, column c should be empty and value for key d in dictionary should  be discarded.

    td2:(`a`b`d)!(99;`a;21)
    
    x:([]a:1 2 3;b:`I`J`K;c:10 20 30)
    jhug replied 7 months ago 3 Members · 2 Replies
  • 2 Replies
  • rocuinneagain

    Member
    April 22, 2022 at 12:00 am
    q){k:key y;x upsert enlist (k where k in cols x)#y}[x;td2] 
    a b c 
    ------- 
    1 I 10 
    2 J 20 
    3 K 30 
    99 a
  • Laura

    Administrator
    April 25, 2022 at 12:00 am
    q)x upsert td2 
    cols x 
    a b c 
    ------- 
    1 I 10 
    2 J 20 
    3 K 30 
    99 a

Log in to reply.