cancel
Showing results for 
Search instead for 
Did you mean: 

Conditional Upsert

hoffmanroni
New Contributor III
Hi Guys, 

Whats the best way to conditionally upsert two tables?  For example if I have two tables

t:`sym xkey flip (`sym`val)!(`a`b`c;(1;3;5))
t1:`sym xkey flip (`sym`val)!(`b`c`d;(8;1;9))

and I want to only update values in table t when there is a higher value in table t1, so final table should be

sym val
a    1
b    8
c    5
d    9

I know change col name of t1,  uj them, then check which value is higher 

select val:last max(val;val1) by sym from (t uj (select val1:last val by sym from t1))


but it seems like there should be a better way

Thanks
3 REPLIES 3

wp
New Contributor
q)max(t;t1)sym| val---| ---a | 1b | 8c | 5d | 9On Wed, Sep 27, 2017 at 9:48 PM, Roni Hoffman wrote:> Hi Guys,>> Whats the best way to conditionally upsert two tables? For example if I> have two tables>> t:`sym xkey flip (`sym`val)!(`a`b`c;(1;3;5))> t1:`sym xkey flip (`sym`val)!(`b`c`d;(8;1;9))>> and I want to only update values in table t when there is a higher value in> table t1, so final table should be>> sym val> a 1> b 8> c 5> d 9>> I know change col name of t1, uj them, then check which value is higher>> select val:last max(val;val1) by sym from (t uj (select val1:last val by sym> from t1))>>> but it seems like there should be a better way>> Thanks>> --> You received this message because you are subscribed to the Google Groups> "Kdb+ Personal Developers" group.> To unsubscribe from this group and stop receiving emails from it, send an> email to personal-kdbplus+unsubscribe@googlegroups.com.> To post to this group, send email to personal-kdbplus@googlegroups.com.> Visit this group at https://groups.google.com/group/personal-kdbplus.> For more options, visit https://groups.google.com/d/optout.

Attila
New Contributor
q)t|t1
sym| val
---| ---
a  | 1
b  | 8
c  | 5
d  | 9

q)t|:t1
q)t
sym| val
---| ---
a  | 1
b  | 8
c  | 5
d  | 9

hoffmanroni
New Contributor III
haha 😐 , ok thanks