# Applying nested conditions in  q

**URL:** https://forum.kx.com/t/applying-nested-conditions-in-q/13469
**Category:** Community Support
**Tags:** kdb-and-q, imported
**Created:** [September 23, 2021, 12:00am UTC](https://forum.kx.com/t/applying-nested-conditions-in-q/13469 "2021-09-23T00:00:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lkerr2](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/lkerr2/32/57_2.png) [@lkerr2](https://forum.kx.com/u/lkerr2)
#### Post date: [September 23, 2021, 12:00am UTC](https://forum.kx.com/t/applying-nested-conditions-in-q/13469/1 "2021-09-23T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/applying-nested-conditions-in-q](https://learninghub.kx.com/forums/topic/applying-nested-conditions-in-q)

I want to add a column to an existing table based on nested conditions

T:(a:`F`R;b:10 20;c:9 21;d:10 22)

Func:{[tab]

If a=`F

If b\<c

Add new column with value as 'hi

Else

new='welcome

Else

if c\>d

new=`hi

Else

new='welcome

}

Please help.

---

<div class="post-metadata">

### Author: ![cillianreilly](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@cillianreilly](https://forum.kx.com/u/cillianreilly)
#### Post date: [September 23, 2021, 12:00am UTC](https://forum.kx.com/t/applying-nested-conditions-in-q/13469/2 "2021-09-23T00:00:00Z")

</div>

This is most easily done in two parts and then defaulting the rest to welcome:

&nbsp;

```
q)T:update newCol:`hi from T where a=`F,b`F,c>d 
q)T:update`welcome^newCol from T 
q)T 
a b c d newCol 
------------------ 
F 10 9 10 welcome 
R 20 21 22 welcome
```

&nbsp;

You may find the reference on update statements useful: [https://code.kx.com/q/ref/update/](https://code.kx.com/q/ref/update/")

If you want it done in one line (which doesn’t scale well for any additional clauses you might want):

&nbsp;

```
q)update newCol:`welcome`hi any(all(a=`F;b`F;c>d))from T 
a b c d newCol 
------------------ 
F 10 9 10 welcome 
R 20 21 22 welcome
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

### Author: ![dwalshq](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@dwalshq](https://forum.kx.com/u/dwalshq)
#### Post date: [September 23, 2021, 12:00am UTC](https://forum.kx.com/t/applying-nested-conditions-in-q/13469/3 "2021-09-23T00:00:00Z")

</div>

Another option is to use vector conditional (?)

[https://code.kx.com/q/ref/vector-conditional/](https://code.kx.com/q/ref/vector-conditional/")

```
q)update new_col:?[a=`F;[?[bd;`hi;`welcome]]] from T 
a b c d new_col 
------------------ 
F 10 9 10 welcome 
R 20 21 22 welcome
```
