function not working properly

52 views
Skip to first unread message

Sam

unread,
Oct 6, 2012, 7:31:59 PM10/6/12
to personal...@googlegroups.com
I am confused with this:  Look at this below. Why are the lines in bold yielding different results?

q)trade
date       open  high  low   close volume   sym
------------------------------------------------
2006.10.03 24.5  24.51 23.79 24.13 19087300 AMD
2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
2006.10.03 24.1  25.1  23.95 25.03 17869600 AMD
2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
2006.10.03 24.8  25.24 24.6  25.11 17304500 AMD
2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
2006.10.03 24.66 24.8  23.96 24.01 17299800 AMD
2006.10.03 27.76 28    27.65 27.87 36452200 MSFT
q)extr
{[t;c;r] select from t where (`$1#'string c) within r}
q)
q)
q)extr[trade;sym;`K`Z]
date       open  high  low   close volume   sym
------------------------------------------------
2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
q)
q)
q)select from trade where (`$1#'string sym) within `K`Z
date       open  high  low   close volume   sym
------------------------------------------------
2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
2006.10.03 27.76 28    27.65 27.87 36452200 MSFT


Bohák András

unread,
Oct 7, 2012, 1:04:31 AM10/7/12
to personal...@googlegroups.com
Hi Sam,
you can not use an input of the function as a col name in a query like this. What happens when you run "extr[trade;sym;`K`Z]" is that the existing variable named sym will be passed to the function. Try "extr[trade;open;`1`5]", it will not even run, if you do not have "open" defined (you will get 'open).
The only way (that I know) to have a query with a dynamic column name is to use the functional form of select. This will work as intended:

extr:{?[x;enlist (within;($;enlist `;((';#);1;(string;y)));enlist z);0b;()]}

When you write functional forms it is usually a huge help to parse the query in q and write the functional form based on the result.
Note that the enlist before "z" is necessary because otherwise q would interpret `K`Z (or whatever limits) as column names.

Best,
Andras
> --
> You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/personal-kdbplus/-/hGNfaxC-Nm0J.
> To post to this group, send email to personal...@googlegroups.com.
> To unsubscribe from this group, send email to personal-kdbpl...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/personal-kdbplus?hl=en.

Sam

unread,
Oct 7, 2012, 3:23:14 AM10/7/12
to personal...@googlegroups.com
Thanks for the information. It makes sense to me. How do I view functional form of query generated by Q?

thanks,
Sam

Sam

unread,
Oct 7, 2012, 3:40:38 AM10/7/12
to personal...@googlegroups.com
I found the function 'parse'. Thanks. This is helpful.

Bohák András

unread,
Oct 7, 2012, 3:41:38 AM10/7/12
to personal...@googlegroups.com
parse "select from trade where (`$1#'string sym) within `K`Z"
> To view this discussion on the web visit https://groups.google.com/d/msg/personal-kdbplus/-/9DxjIUgo5_MJ.
Reply all
Reply to author
Forward
0 new messages