# apply  folding over rows of a table

**URL:** <https://forum.kx.com/t/apply-folding-over-rows-of-a-table/6535>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [March 24, 2010, 4:18pm UTC](https://forum.kx.com/t/apply-folding-over-rows-of-a-table/6535 "2010-03-24T16:18:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julien\_Laugel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Julien\_Laugel](https://forum.kx.com/u/Julien_Laugel)\
**Post date:** [March 24, 2010, 4:18pm UTC](https://forum.kx.com/t/apply-folding-over-rows-of-a-table/6535/1 "2010-03-24T16:18:00Z")

</div>

Hi,(1st post so excuses in advance for the potential newbyness of thequestion)I’d like to fold a function, i.e. to use the over adverb (/) , foreach rows of a table.Here’s my try :unary\_corporate\_action:{[x;y] update price:price \* y`coef from x wheredate \< y`date}; / for atbl unary\_unary\_corporate\_action/ corporate\_action;where tbl is a price table, and corporate\_action is a table ofcorporate\_actionWhat I’d wanted was to recursively apply the unary function to theinitial tblThe result is a 'nyi error message (not yet implemented)what’s the difference wrt a100 {2\*x+y}/ 1 2 3that works?or wrt a{(x`price) +1} each tblthat also works?(well, there are obviously differences, but what particular pointmakes it “nyi”?)Do I have smarter alternatives than using a for loop?Many thanks in advanceJulien

---

<div class="post-metadata">

**Author:** ![gabriel\_ricardo](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@gabriel\_ricardo](https://forum.kx.com/u/gabriel_ricardo)\
**Post date:** [March 26, 2010, 11:20pm UTC](https://forum.kx.com/t/apply-folding-over-rows-of-a-table/6535/2 "2010-03-26T23:20:00Z")

</div>

Hi Julien,If I understand what you’re asking, you want a function to look atrows from two different tables and update a column in one of thetables.I don’t think your problem is with applying the function over a table,it’s with the usage of two tables in a single “update..from” query.You can reference another table in a table query if you use a sharedkey to look-up the row. For example if I have one table "tblprice"and another table “tblecoef”, I can do something very similar to yourupdate query like this:update price:price\*tblcoef[(sym);`coef] from tblpriceNotice the syntax for referencing the second table ("tblcoef") whendoing a query on tblprice.If I only wanted specific rows from tblcoef, I could query those firstand store them in a temporary table for use later, like this:tmptbl:select from tblcoef where sym in `ABC`DEF`GHI`JKLThen run the similar query as above:update price:price*1^tmptbl[([]sym);`coef] from tblpriceBelow is an example I tried.Hope that helps.q)tblprice:(([sym:`ABC`DEF`GHI`JKL`MNO`PQR`STU`QWE`RTY`CVB]price:10+10?10f);q)tblpricesym| price—| --------ABC| 13.92752DEF| 15.17091GHI| 15.1598JKL| 14.06664MNO| 11.78084PQR| 13.01772STU| 17.85033QWE| 15.3471RTY| 17.11172CVB| 14.11597q)tblcoef:([sym:`ABC`DEF`GHI`JKL`MNO`PQR`STU`QWE`RTY`CVB]coef:10?1f)q)tblcoefsym| coef—| ----------ABC| 0.4931835DEF| 0.5785203GHI| 0.08388858JKL| 0.1959907MNO| 0.375638PQR| 0.6137452STU| 0.5294808QWE| 0.6916099RTY| 0.2296615CVB| 0.6919531q)update price:price\*tblcoef[(sym);`coef] from tblpricesym| price---| --------ABC| 6.868825DEF| 8.77668GHI| 1.271734JKL| 2.756931MNO| 4.42533PQR| 7.989564STU| 9.451407QWE| 10.6142RTY| 3.929903CVB| 9.767589On Mar 24, 9:18?am, Julien Laugel <jlau...> wrote:&gt; Hi,&gt; (1st post so excuses in advance for the potential newbyness of the&gt; question)&gt;&gt; I'd like to fold a function, i.e. to use the over adverb (/) , for&gt; each rows of a table.&gt;&gt; Here's my try :&gt; unary_corporate_action:{[x;y] update price:price * y`coef from x where\> date \< y`date}; ?/ for a&gt; tbl unary_unary_corporate_action/ corporate_action;&gt;&gt; where tbl is a price table, and corporate_action is a table of&gt; corporate_action&gt;&gt; What I'd wanted was to recursively apply the unary function to the&gt; initial tbl&gt;&gt; The result is a 'nyi ?error message (not yet implemented)&gt;&gt; what's the difference wrt a&gt; 100 {2*x+y}/ 1 2 3&gt; that works?&gt; or wrt a&gt; {(x`price) +1} each tbl\> that also works?\> (well, there are obviously differences, but what particular point\> makes it “nyi”?)\>\> Do I have smarter alternatives than using a for loop?\>\> Many thanks in advance\>\> Julien\</jlau…\>

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [March 26, 2010, 11:33pm UTC](https://forum.kx.com/t/apply-folding-over-rows-of-a-table/6535/3 "2010-03-26T23:33:00Z")

</div>

X-Mailer: Apple Mail (2.936)note that txf joins like this (so-called due to the older form of them which used the txf function) aren’t terribly performant. foreign key joins are much quicker.On Mar 26, 2010, at 7:20 PM, gabber wrote:\> Hi Julien,\> If I understand what you’re asking, you want a function to look at\> rows from two different tables and update a column in one of the\> tables.\> I don’t think your problem is with applying the function over a table,\> it’s with the usage of two tables in a single “update..from” query.\> You can reference another table in a table query if you use a shared\> key to look-up the row. For example if I have one table “tblprice”\> and another table “tblecoef”, I can do something very similar to your\> update query like this:\> update price:price\*tblcoef[(sym);`coef] from tblprice\>\> Notice the syntax for referencing the second table ("tblcoef") when\> doing a query on tblprice.\> If I only wanted specific rows from tblcoef, I could query those first\> and store them in a temporary table for use later, like this:\> tmptbl:select from tblcoef where sym in `ABC`DEF`GHI`JKL\>\> Then run the similar query as above:\> update price:price*1^tmptbl[([]sym);`coef] from tblprice\>\> Below is an example I tried.\> Hope that helps.\>\> q)tblprice:(([sym:`ABC`DEF`GHI`JKL`MNO`PQR`STU`QWE`RTY`CVB]price: \> 10+10?\> 10f);\> q)tblprice\> sym| price\> —| --------\> ABC| 13.92752\> DEF| 15.17091\> GHI| 15.1598\> JKL| 14.06664\> MNO| 11.78084\> PQR| 13.01772\> STU| 17.85033\> QWE| 15.3471\> RTY| 17.11172\> CVB| 14.11597\>\>\> q)tblcoef:([sym:`ABC`DEF`GHI`JKL`MNO`PQR`STU`QWE`RTY`CVB]coef:10?1f)\> q)tblcoef\> sym| coef\> —| ----------\> ABC| 0.4931835\> DEF| 0.5785203\> GHI| 0.08388858\> JKL| 0.1959907\> MNO| 0.375638\> PQR| 0.6137452\> STU| 0.5294808\> QWE| 0.6916099\> RTY| 0.2296615\> CVB| 0.6919531\> q)update price:price\*tblcoef[(sym);`coef] from tblprice\> sym| price\> ---| --------\> ABC| 6.868825\> DEF| 8.77668\> GHI| 1.271734\> JKL| 2.756931\> MNO| 4.42533\> PQR| 7.989564\> STU| 9.451407\> QWE| 10.6142\> RTY| 3.929903\> CVB| 9.767589\>\>\>\> On Mar 24, 9:18 am, Julien Laugel <jlau...> wrote:&gt;&gt; Hi,&gt;&gt; (1st post so excuses in advance for the potential newbyness of the&gt;&gt; question)&gt;&gt;&gt;&gt; I'd like to fold a function, i.e. to use the over adverb (/) , for&gt;&gt; each rows of a table.&gt;&gt;&gt;&gt; Here's my try :&gt;&gt; unary_corporate_action:{[x;y] update price:price * y`coef from x \>\> where\>\> date \< y`date}; / for a&gt;&gt; tbl unary_unary_corporate_action/ corporate_action;&gt;&gt;&gt;&gt; where tbl is a price table, and corporate_action is a table of&gt;&gt; corporate_action&gt;&gt;&gt;&gt; What I'd wanted was to recursively apply the unary function to the&gt;&gt; initial tbl&gt;&gt;&gt;&gt; The result is a 'nyi error message (not yet implemented)&gt;&gt;&gt;&gt; what's the difference wrt a&gt;&gt; 100 {2*x+y}/ 1 2 3&gt;&gt; that works?&gt;&gt; or wrt a&gt;&gt; {(x`price) +1} each tbl\>\> that also works?\>\> (well, there are obviously differences, but what particular point\>\> makes it “nyi”?)\>\>\>\> Do I have smarter alternatives than using a for loop?\>\>\>\> Many thanks in advance\>\>\>\> Julien-- Aaron [Daviesaaron.davies@gmail.com](mailto:Daviesaaron.davies@gmail.com)\</jlau…\>
