# Recursive Query

**URL:** https://forum.kx.com/t/recursive-query/11781
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [June 13, 2018, 6:44pm UTC](https://forum.kx.com/t/recursive-query/11781 "2018-06-13T18:44:00Z")
**Posts on this page:** 9
**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: [June 13, 2018, 6:44pm UTC](https://forum.kx.com/t/recursive-query/11781/1 "2018-06-13T18:44:00Z")

</div>

Hello qExperts, have another quick question if someone can help,

I am trying to have a recursive query where the row value is based on the preceding value in a recursive manner up to that point.&nbsp; So for example

`t1:([]sym:`$();val:“F”$();flag:`$();val2:"F"$());`t1 insert enlist (`sym`val`flag`val2)!(`A;1f;`;0f);`t1 insert enlist (`sym`val`flag`val2)!(`A;2f;`;0f);`t1 insert enlist (`sym`val`flag`val2)!(`A;3f;`B;0f);`t1 insert enlist (`sym`val`flag`val2)!(`A;4f;`;0f);`t1 insert enlist (`sym`val`flag`val2)!(`A;5f;`;0f);sym val flag val2A 1 0A 2 0A 3 B 0A 4 0A 5 0`

Here what I want to do is update val2 recursively based on itself and a different column.&nbsp; So ie if flag=`, then val2 = val+(prev val2), else if flag=`B then val2=prev val2

`sym val flag val2A 1 1A 2 3A 3 B 3A 4 7A 5 12`

Have tried these

`update val2:{[v;f;v2] $[f=`B; prev v2; (prev v2) + v] }'[val;flag;val2] by sym from t1sym val flag val2A 1 1A 2 2A 3 B 0A 4 4A 5 5`

here with each, (prev val2) is not recursive so I can’t get the updated val2 from the previous iteration

`update val2:{[v;f;v2] $[f=`B; prev v2; v2 + v] }[val;flag;val2] by sym from t1sym val flag val2A 1 1 2 3 4 5fA 2 1 2 3 4 5fA 3 B 0fA 4 0fA 5 0f`

here with iterate I get the whole val2 column and not the previously calculated val2

Hope that makes sense, I’m sure I’m missing something here but if you have any ideas would be greatly appreciated

Thanks!

---

<div class="post-metadata">

### Author: ![rathore\_ajay1](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rathore\_ajay1](https://forum.kx.com/u/rathore_ajay1)
#### Post date: [June 13, 2018, 7:07pm UTC](https://forum.kx.com/t/recursive-query/11781/2 "2018-06-13T19:07:00Z")

</div>

If I understand your required output, you can use a simple scan&nbsp;

`q)update val2:{$[z=`B;x;y+x]}[0;val;flag] by sym from t1sym val flag val2-----------------A 1 1A 2 3A 3 B 3A 4 7A 5 12`

---

<div class="post-metadata">

### Author: ![rathore\_ajay1](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rathore\_ajay1](https://forum.kx.com/u/rathore_ajay1)
#### Post date: [June 13, 2018, 7:45pm UTC](https://forum.kx.com/t/recursive-query/11781/3 "2018-06-13T19:45:00Z")

</div>

another way

`q)update val2:sums?[flag=`B;0;val] by sym from t1sym val flag val2-----------------A 1 1A 2 3A 3 B 3A 4 7A 5 12`

---

<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: [June 13, 2018, 7:57pm UTC](https://forum.kx.com/t/recursive-query/11781/4 "2018-06-13T19:57:00Z")

</div>

Thanks Ajay, sorry I think I got turned around and confused myself.&nbsp; I think I have it now and your post helped.&nbsp; Quick question though it looks like the x arg you’re setting to 0 but how come it is the actual value of val2 during the iteration?

---

<div class="post-metadata">

### Author: ![markk1995](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@markk1995](https://forum.kx.com/u/markk1995)
#### Post date: [June 13, 2018, 7:57pm UTC](https://forum.kx.com/t/recursive-query/11781/5 "2018-06-13T19:57:00Z")

</div>

`update val2:sums @[val;where flag=`B;:;0.] by sym from t1`

This saves having to do an if else check every loop

---

<div class="post-metadata">

### Author: ![rathore\_ajay1](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rathore\_ajay1](https://forum.kx.com/u/rathore_ajay1)
#### Post date: [June 13, 2018, 8:11pm UTC](https://forum.kx.com/t/recursive-query/11781/6 "2018-06-13T20:11:00Z")

</div>

Well, 0 is just setting the seed, and in each iteration either it is&nbsp;using the unmodified value from previous iteration or adding val&nbsp;to it. It is essentially folding the lambda onto y and z.

---

<div class="post-metadata">

### Author: ![rathore\_ajay1](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@rathore\_ajay1](https://forum.kx.com/u/rathore_ajay1)
#### Post date: [June 13, 2018, 9:43pm UTC](https://forum.kx.com/t/recursive-query/11781/7 "2018-06-13T21:43:00Z")

</div>

Not sure who is moderating this group these days, but&nbsp;noticed lately that replies are getting delayed sometimes by 2 to 3 hours and seems to appear out of order.

---

<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: [June 13, 2018, 10:01pm UTC](https://forum.kx.com/t/recursive-query/11781/8 "2018-06-13T22:01:00Z")

</div>

Thanks you Ajay, much appreciated

---

<div class="post-metadata">

### Author: ![jwbuitenhuis](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@jwbuitenhuis](https://forum.kx.com/u/jwbuitenhuis)
#### Post date: [June 14, 2018, 8:06am UTC](https://forum.kx.com/t/recursive-query/11781/9 "2018-06-14T08:06:00Z")

</div>

How about:

update val2:sums val\*`B`?flag from t1
