2022.10.08 09:55 PM - edited 2022.10.09 02:31 AM
tab:( c1: til 10 ; c2:10 + til 10) /Both do not work ?[tab;til 10;(>;`c2;16)] ?[tab;til 10;(last `c2)]
2022.10.09 02:53 AM
Simply prepend where to the former
q)where ?[tab;til 10;(>;`c2;16)] 7 8 9
and insert a semicolon to the latter
q)?[tab;til 10;(last;`c2)] 19
2022.10.11 06:52 AM
Where in the doc does it say a "where" is needed ?
Similarly adding ";" is not necessary in the examples shown in docs.
@SJT, Are these both cases of incomplete documentation ?
2022.10.11 09:10 AM
It's not entirely clear what you are trying to achieve when you only supply two broken queries.
Adding where to your query was a suggestion based on the return type being a list of booleans. Using where with your query returns the indexes of tab where c2>16. This could then be fed back into the table to return the desired data.
q)tab where ?[tab;til 10;(>;`c2;16)] c1 c2 ----- 7 17 8 18 9 19
You'll need to use semicolons to form a valid parse tree as shown here https://code.kx.com/q/wp/parse-trees/#eval-and-value like you have used in your first query but not your second.
This was all based off the assumption that you were attempting to use simple execs in functional form https://code.kx.com/q/basics/funsql/#simple-exec
2022.10.11 07:08 PM
You can also include where in the parse tree passed as the last argument.
q)?[tab;til 10;(where;(>;`c2;16))] 7 8 9
2022.10.11 10:40 PM - edited 2022.10.12 12:21 AM
In response to @cillianreilly and @mchbrn-q
It seems, simple exec parse tree has a dual nature : It can act as a "c" and an "a" in ?[t;c;b;a] .
It seems like a "c" here:
?[tab;til 10;(where;(>;`c2;16))] /Equivalent ?[tab;til 10;((&:);(>;`c2;16))]
It seems like an "a" here:
2022.10.12 10:47 AM
Yeah that's interesting. And since "a", the select phrase, can take a parse tree, you could translate the functional simple exec
q)?[tab;til 10;(last;`c2)] 19
to the functional exec
q)exec last c2 from tab 19 q)parse "exec last c2 from tab" ? `tab () () ,(last;`c2) q)?[tab;();();(last;`c2)] 19
I can't find any examples online of standard qSQL simple execs though? I don't know if it exists because https://code.kx.com/q/ref/exec/#syntax doesn't reference any phrases that allows for indexes . . .
2022.10.12 12:20 PM - edited 2022.10.12 12:22 PM
Lets us ask the all knowing wizard , the mighty @SJT 😀 about the dual nature mentioned in my post above.
2022.10.09 04:16 PM
As you've mentioned parse trees in your post title, I'll limit the scope of the following examples here using parse on a select statement*;
If you want to carry out of a functional select for both of the queries above, I suggest using the parse keyword to determine the components of the phrase:
// c2>16 example q)parse "select from tab where c2>16" ? `tab ,,(>;`c2;16) 0b () q)?[tab;enlist (>;`c2;16);0b;()] c1 c2 ----- 7 17 8 18 9 19 // last c2 example q)parse "select last c2 from tab" ? `tab () 0b (,`c2)!,(last;`c2) q)?[tab;();0b;enlist[`c2]!enlist (last;`c2)] c2 -- 19
You can read more about parse here and parse trees here
*There are other ways of retrieving specific values for the queries you've listed. For example, you could also write last tab[`c2] to fetch the last value in the c2 column
Hope this helps.
2022.10.10 04:35 AM
Would just like to add https://code.kx.com/q/basics/funsql/#simple-exec
Judging by the number of args in OP's functional query, a simple exec takes table name, list of indexes and parse tree.
Tel: +44 (0)28 3025 2242
Tel: +1 (212) 447 6700
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.