# Conditional Upsert

**URL:** <https://forum.kx.com/t/conditional-upsert/11614>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [September 27, 2017, 9:48pm UTC](https://forum.kx.com/t/conditional-upsert/11614 "2017-09-27T21:48:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoffmanroni1](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@hoffmanroni1](https://forum.kx.com/u/hoffmanroni1)\
**Post date:** [September 27, 2017, 9:48pm UTC](https://forum.kx.com/t/conditional-upsert/11614/1 "2017-09-27T21:48:00Z")

</div>

Hi Guys,&nbsp;

Whats the best way to conditionally upsert two tables?&nbsp; 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 vala 1b 8c 5d 9`

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

`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

---

<div class="post-metadata">

**Author:** ![wp1](https://avatars.discourse-cdn.com/v4/letter/w/db5fbb/32.png) [@wp1](https://forum.kx.com/u/wp1)\
**Post date:** [September 27, 2017, 10:04pm UTC](https://forum.kx.com/t/conditional-upsert/11614/2 "2017-09-27T22:04:00Z")

</div>

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\>\> –\>

Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![Attila](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Attila](https://forum.kx.com/u/Attila)\
**Post date:** [September 27, 2017, 10:05pm UTC](https://forum.kx.com/t/conditional-upsert/11614/3 "2017-09-27T22:05:00Z")

</div>

q)t|t1

sym| val

—| —

a &nbsp;| 1

b &nbsp;| 8

c &nbsp;| 5

d &nbsp;| 9

q)t|:t1

q)t

sym| val

—| —

a &nbsp;| 1

b &nbsp;| 8

c &nbsp;| 5

d &nbsp;| 9

>

---

<div class="post-metadata">

**Author:** ![hoffmanroni1](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@hoffmanroni1](https://forum.kx.com/u/hoffmanroni1)\
**Post date:** [September 27, 2017, 11:08pm UTC](https://forum.kx.com/t/conditional-upsert/11614/4 "2017-09-27T23:08:00Z")

</div>

haha :| , ok thanks&nbsp;
