Column dependencies

Hi,I’m trying to add a number of calculated columns that depend on eachother, but this doesn’t work. (I have simplified the calculationshere.)b:([sym:ab] open:10 22);t:( sym:b$ab; price:11 22);select sym,price,return:price - sym.open,vol:return xexp 2 from tIs there a way to return multiple columns from a function, somethinglike the following?select sym,price,return`vol!{r:x - y;(r;r xexp 2)}[price;sym.open]from tThe only way I have been able to get something close to what I want isto nest the queries which isn’t easy in my case.select sym,price,return,vol:return xexp 2 from selectsym,price,return:price - sym.open from tThanks in advance,Victor

On Mar 18, 2:07?pm, Victor Wong <victor.w…> wrote:> select sym,price,returnvol!{r:x - y;(r;r xexp 2)}[price;sym.open]> from t>> The only way I have been able to get something close to what I want is> to nest the queries which isn’t easy in my case.>> select sym,price,return,vol:return xexp 2 from select> sym,price,return:price - sym.open from t![t;();0b;cd!({(x+y;x-y)};a;b)]or![t;();0b;(cd!{(x+y;x-y)};a;b)]ought to work, but neither does. unfortunately i think the dictionaryis required at the top level and its value side is count-checkedbefore execution.anyone know a way around this?</victor.w…>

This (without the column names) is what you want, I think: exec (,/)((sym;price);f[price;sym.open]) from twhere f is {r:x - y;(r;r xexp 2)}, Then it should be possible toadd the column names using a functional form. Unfortunatelywhile parse "exec (,/)((sym;price);f[price;sym.open]) from t"returns?t()(),((/;,);(plist;(plist;sym;price);(f;price;sym.open)))if you actually try it, i.e., ?[t;();();((/;,);(plist;(plist;`sym;`price);(`f;`price;`sym.open)))]you get a “'(” error, which is baffling since theparenthesizing is correct and it is exactly theparse output. Looks like a bug to me.On Mar 18, 8:46?pm, Aaron Davies <aaron.dav…> wrote:> On Mar 18, 2:07?pm, Victor Wong <victor.w…> wrote:>> > select sym,price,`return`vol!{r:x - y;(r;r xexp 2)}[price;sym.open]> > from t>> > The only way I have been able to get something close to what I want is> > to nest the queries which isn’t easy in my case.>> > select sym,price,return,vol:return xexp 2 from select> > sym,price,return:price - sym.open from t>> ![t;();0b;`c`d!({(x+y;x-y)};`a;`b)]>> or>> ![t;();0b;(`c`d!{(x+y;x-y)};`a;`b)]>> ought to work, but neither does. unfortunately i think the dictionary> is required at the top level and its value side is count-checked> before execution.>> anyone know a way around this?</victor.w…></aaron.dav…>

To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1077)

update vol:return xexp 2 from update return:price-sym.open from t
should be ok

but if you really insist
t:t,'flipvolreturn!{(r xexp 2;r:x-y)}[t.price;exec sym.open from t]

Regards,
Attila
On 18 Mar 2010, at 18:07, Victor Wong wrote:

> Hi,
>
> I’m trying to add a number of calculated columns that depend on each
> other, but this doesn’t work. (I have simplified the calculations
> here.)
>
> b:([sym:ab] open:10 22);
> t:( sym:b$ab; price:11 22); \> select sym,price,return:price - sym.open,vol:return xexp 2 from t \> \> Is there a way to return multiple columns from a function, something \> like the following? \> \> select sym,price,return`vol!{r:x - y;(r;r xexp 2)}[price;sym.open]
> from t
>
> The only way I have been able to get something close to what I want is
> to nest the queries which isn’t easy in my case.
>
> select sym,price,return,vol:return xexp 2 from select
> sym,price,return:price - sym.open from t
>
> Thanks in advance,
> Victor
>
> –
> You received this message because you are subscribed to the Google =
Groups “Kdb+ Personal Developers” group.
> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
> To unsubscribe from this group, send email to =
personal-kdbplus+unsubscribe@googlegroups.com.
> For more options, visit this group at =
http://groups.google.com/group/personal-kdbplus?hl=en.
>

> ?[t;();();((/;,);(plist;(plist;sym;price);(f;price;sym.open)))]this worksq)?[t;();();(raze;(plist;(plist;sym;price);(f;price;sym.open)))]a b11 221 01 0

Thanks. It’s really frustrating how parse output cannotreliably be used to construct functional forms, eventhough AFAICT that is its only practical use.Anyway, there is still something I don’t understand. Letr: ?[t;();();(raze;(plist;(plist;sym;price);(f;price;sym.open)))]then?[t;();0b;sympricereturnvol!r]gives the desired result butq)?[t;();0b;sympricereturnvol!(raze;(plist;(plist;sym;price);(f;price;sym.open)))]fails with a length error. Why?Olivier

Thanks, everyone, for the quick and helpful answers. I added thecolumn names and am now happily using the following:flip sympriceretvol!?[t;();();(,;(plist;sym;price);(f;price;sym.open))]VictorOn Mar 19, 4:43?am, Aaron Davies <aaron.dav...> wrote:&gt; &gt; ?[t;();();((/;,);(plist;(plist;sym;price);(f;price;sym.open)))]>> this works>> q)?[t;();();(raze;(plist;(plist;`sym;`price);(`f;`price;`sym.open)))]> a ?b> 11 22> 1 ?0> 1 ?0</aaron.dav…>

'length comes from the dictionary literal in the fourth argument of
?–it contains four keys and two values. It has to be a valid
dictionary at the time the select begins, not after
the value side is executed.

Complicated expressions in the 4th argument of afunctional select are common. It seems to me thatif you were right we would be limited to plain-Janeselects in which you merely rearrange columns.What is it *specifically* about this expression thatmakes it somehow invalid in this context?

Answering my own question, perhaps you mean thatthe expression to the right of the bang sign has to be"obviously" of length 4?

Hi Oliver,

I guess that Aaron means that the standalone expression is invalid:

q)sympricereturnvol!(raze;(plist;(plist;sym;price);(f;price;`sym.open)))

'length

i.e. both sides of an association should be lists of the same length.

q)count (raze;(plist;(plist;sym;price);(f;price;`sym.open)))

2

q)(raze;(plist;(plist;sym;price);(f;price;`sym.open)))

,/

(plist;(plist;sym;price);fprice`sym.open)

The left hand side has count 4, while the right has 2.

Regards,

Andrew

Yeah, basically. Select could conceptually have something like
(!;sympricereturnvol;(raze;(plist;(plist;sym;price);(f;price;`sym.open))))
as its entire fourth arg, where the dictionary isn’t constructed until
the values have been calculated, bit that’s not how it works at the
moment–the column dict has to be a valid dict from the beginning.

OK, thanks. Another entry for my growing collection of scoping/parsing/evaluation quirks…