max value of pref N rows

Hi,

table t  is like this:

AB

???

a1

a2

a6

a1

a7

b1

b2

b3

b2

b1

I want to get a table like this

AB max B of prev 2 rows

???

a1-

a2-

a62

a16

a76

b1-

b2-

b32

b23

b13

Please help, Thanks!

Roy

update n:@[;1;:;0N] max 1_prev[2;B] by A from flipAB!(aaaaabbbbb ;1 2 6 1 7 1 2 3 2 1)<o:p></o:p>

<o:p> </o:p>

HTH,<o:p></o:p>

<o:p> </o:p>

Kim<o:p></o:p>

<o:p> </o:p>

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von liuchuanbo@gmail.com
Gesendet: Montag, 15. September 2014 17:03
An: Kdb+ Personal Developers
Betreff: [personal kdb+] max value of pref N rows<o:p></o:p>

<o:p> </o:p>

Hi,<o:p></o:p>

<o:p> </o:p>

table t  is like this:<o:p></o:p>

<o:p> </o:p>

A         B<o:p></o:p>

——————<o:p></o:p>

a          1<o:p></o:p>

a          2<o:p></o:p>

a          6<o:p></o:p>

a          1<o:p></o:p>

a          7<o:p></o:p>

b          1<o:p></o:p>

b          2<o:p></o:p>

b          3<o:p></o:p>

b          2<o:p></o:p>

b          1<o:p></o:p>

<o:p> </o:p>

I want to get a table like this <o:p></o:p>

A         B         max B of prev 2 rows <o:p></o:p>

——————<o:p></o:p>

a          1          -<o:p></o:p>

a          2          -<o:p></o:p>

a          6          2<o:p></o:p>

a          1          6<o:p></o:p>

a          7          6<o:p></o:p>

b          1          -<o:p></o:p>

b          2          -<o:p></o:p>

b          3          2<o:p></o:p>

b          2          3<o:p></o:p>

b          1          3<o:p></o:p>

<o:p> </o:p>

Please help, Thanks!<o:p></o:p>

<o:p> </o:p>

–<o:p></o:p>

Roy<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>


Submitted via Google Groups

Should use mmax here.

update n:2 mmax prev B by A from tab

Then you can null out the first two items per group if you really need to.

Terry

<o:p> </o:p>

>>> Should use mmax here. <o:p></o:p>

<o:p> </o:p>

q)update n:@[;1;:;0N]max 1_2 prev\B by A from flipAB!(aaaaabbbbb ;1 2 6 1 7 1 2 3 2 1)<o:p></o:p>

q)update n:@[;0 1;:;0N]2 mmax prev B by A from flipAB!(aaaaabbbbb ;1 2 6 1 7 1 2 3 2 1)<o:p></o:p>

<o:p> </o:p>

My solution is one character smaller J<o:p></o:p>

<o:p> </o:p>

Kim<o:p></o:p>

<o:p> </o:p>

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Terry Lynch
Gesendet: Montag, 15. September 2014 17:39
An: personal-kdbplus@googlegroups.com
Betreff: Re: AW: [personal kdb+] max value of pref N rows<o:p></o:p>

<o:p> </o:p>

Should use mmax here. <o:p></o:p>

update n:2 mmax prev B by A from tab<o:p></o:p>

Then you can null out the first two items per group if you really need to. <o:p></o:p>

Terry<o:p></o:p>

On Sep 15, 2014 11:28 AM, “Kim Tang” <kuentang@vodafone.de> wrote:<o:p></o:p>

update n:@[;1;:;0N] max 1_prev[2;B] by A from flipAB!(aaaaabbbbb ;1 2 6 1 7 1 2 3 2 1)<o:p></o:p>

 <o:p></o:p>

HTH,<o:p></o:p>

 <o:p></o:p>

Kim<o:p></o:p>

 <o:p></o:p>

Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von liuchuanbo@gmail.com
Gesendet: Montag, 15. September 2014 17:03
An: Kdb+ Personal Developers
Betreff: [personal kdb+] max value of pref N rows<o:p></o:p>

 <o:p></o:p>

Hi,<o:p></o:p>

 <o:p></o:p>

table t  is like this:<o:p></o:p>

 <o:p></o:p>

A         B<o:p></o:p>

——————<o:p></o:p>

a          1<o:p></o:p>

a          2<o:p></o:p>

a          6<o:p></o:p>

a          1<o:p></o:p>

a          7<o:p></o:p>

b          1<o:p></o:p>

b          2<o:p></o:p>

b          3<o:p></o:p>

b          2<o:p></o:p>

b          1<o:p></o:p>

 <o:p></o:p>

I want to get a table like this <o:p></o:p>

A         B         max B of prev 2 rows <o:p></o:p>

——————<o:p></o:p>

a          1          -<o:p></o:p>

a          2          -<o:p></o:p>

a          6          2<o:p></o:p>

a          1          6<o:p></o:p>

a          7          6<o:p></o:p>

b          1          -<o:p></o:p>

b          2          -<o:p></o:p>

b          3          2<o:p></o:p>

b          2          3<o:p></o:p>

b          1          3<o:p></o:p>

 <o:p></o:p>

Please help, Thanks!<o:p></o:p>

 <o:p></o:p>

–<o:p></o:p>

Roy<o:p></o:p>

 <o:p></o:p>

 <o:p></o:p>


Submitted via Google Groups

Thanks Kim & Terry!

using “til x” maybe better :)

q)t:flipAB!(aaaaabbbbb ;1 2 6 1 7 1 2 3 2 1)

q)f:{update n:@[;til x;:;0N]x mmax prev B by A from t}

q)f[2]

you might want

@[x;til x&count i;:;x 0N]

to deal with the edge cases of:

less than two rows per symbol

different numeric types (including date/time)

nested

  Attila