Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- kdb+ and q
- :
- Iterating and calculating values based on previous

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Iterating and calculating values based on previous

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.02 11:52 AM

I know I've done this before and seem to have gotten stuck again. Wondering if someone can help me out. I want to calculate a columnar value based on its previous value at some index before it.

So if I have table t

`t:([]val: 10#0f)`

val0000000000

and I want to add 1 to the value also add the prev value, this doenst work but tried

`update val:{x+y+1}\[val;prev val] from t`

result would be

`val12345678910`

and would like to be able to have the prev value based on some index, so for example something like

`update val:{x+y+1}\[val;5 xprev val] from t`

`val1111234567`

Hope that makes sense. Basically iterating through a column and calculating the new values based on some previous index.

Thanks!

10 REPLIES 10

User

Not applicable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.02 12:06 PM

`q)update val:sums val+1 from t`

The second question

`q)update val:1+ sums val + 5 xprev (val + 1) from t`

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.03 01:45 AM

sums works nicely in this case as Callum showed, in the general case you're probably thinking of

`q)update col2:{x+y+1}\[0;col] from tcol col2--------8 91 119 215 274 326 396 461 488 575 63`

You were missing a seed value (zero).

Terry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.03 04:45 AM

`update col2:{x+y+1}\[0;col] from t`

what does the 0 represent here?

Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.03 06:56 AM

prevFunctionResult + currentColumnEntry + 1

but for the first iteration there is no prevFunctionResult so you can set to a value of your choosing - I assumed you would want zero. This would seed it with 1000:

`update col2:{x+y+1}\[1000;col] from t`

I'm not 100% clear what you're looking for with the xprev.....are you looking for:

prevFunctionResult + columnEntryFiveRowsBefore + 1

? If so then you can do

`update col2:{x+y+1}\[0;0^5 xprev col] from t`

But this is inconsistent with the example posted originally. I'm not sure if your original example is logically consistent with an accumulator!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.03 08:32 AM

So when I do this

`update col2:{x+y+1}\[0;0^1 xprev col] from t`

shouldn't x=y? I dont see that

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.04 08:15 AM

Use 0N! to see the intermediary values of x and y

`q)update col2:{0N!(x;y);x+y+1}\[0;0^1 xprev col] from t;0 01 810 112 922 528 433 640 647 149 8`

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.05 05:53 AM

so what am really looking for is **prevFuncResult5IterationsAgo** or some x number of iterations ago. So I see seeding the value with 0 works for getting **prevFuncResult** but I can't seem to get anything before the prev result.

If I use

`q)update col2:{x+y+1}\[0;0^1 xprev col] from t;`

Thanks so much

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.05 06:59 AM

q)t:([]col:8 1 9 5 4 6 6 1 8 5)

/current column value plus 5th previous function output plus one

q)update col2:last each {neg[x]#(x#0),y,1+y[0]+z}[5]\[(),0;col] from t

col col2

--------

8 9

1 2

9 10

5 6

4 5

6 16

6 9

1 12

8 15

5 11

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.05 08:12 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2019.07.05 09:23 AM

You can shorten it a little bit

`q)update col2:last each{1_x,1+y+x 0}\[5#0;col] from tcol col2--------8 91 29 105 64 56 166 91 128 155 11`

-Ajay

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Resources

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.