Search code examples
kdb

Joining dictionaries to make a table


I get a table when I join the dictionaries with same keys :

q)((`s`p!`s1`p1);(`s`p!`s2`p2))

s  p 
-----
s1 p1
s2 p2

But if keys are different it just returns the list, is there a way to generate a proper table?

q)((`s`p!`s1`p1);(`s`p`m!`s2`p2`m2))

`s`p!`s1`p1
`s`p`m!`s2`p2`m2

Solution

  • You can use uj along with adverb over to get the table:

    q)(uj)over enlist each ((`s`p!`s1`p1);(`s`p`m!`s2`p2`m2))
    
    s  p  m 
    --------
    s1 p1   
    s2 p2 m2
    

    Please note that we first made the individual dictionary to table before joining.

    The over is particularly useful when there are more than 2 dictionaries. Otherwise using uj alone can do the trick.

    q)(enlist `s`p!`s1`p1) uj (enlist `s`p`m!`s2`p2`m2)
    
    s  p  m 
    --------
    s1 p1   
    s2 p2 m2