cancel
Showing results for 
Search instead for 
Did you mean: 

Insert a not matching dictionary to a table

jhug
New Contributor

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)

2 ACCEPTED SOLUTIONS

rocuinneagain
Contributor III
Contributor III
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

View solution in original post

SJT
Contributor III
Contributor III
q)x upsert td2 cols x
a  b c
-------
1  I 10
2  J 20
3  K 30
99 a

View solution in original post

2 REPLIES 2

rocuinneagain
Contributor III
Contributor III
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

SJT
Contributor III
Contributor III
q)x upsert td2 cols x
a  b c
-------
1  I 10
2  J 20
3  K 30
99 a