Hi,
I am learning the basic syntax of q. Just now I learned about exec which really confused me a lot.
=========my question =========
What’s the difference between “exec” and “select”? I noticed that the table returned by “exec” doesn’t have a column header unless notified. Is this the only difference?
I noticed a sentence on this page http://code.kx.com/wiki/Reference/exec saying that “select is always 0b/dict, keyed select is always dict/dict (becomes the last two execs above)”, could you please explain a little about this sentence? I don’t really understand what do “0b/dict” and “dict/dict” mean.
Thanks in advance!
Xinyu
some details for exec vs select are on this page:
http://stackoverflow.com/questions/25409127/difference-between-exec-and-select-prime-factorization-algorithm
Following 2 statements gives the same result (normal select is equal to exec with grouping clause ‘by’ having false value)
q) select from table
q) exec by 0b from table
Keyed table is mapping from dictionary to dictionary where keyed columns make one dictionary and rest of the columns make second dictionary.
That’s why keyed select is dict/dict which means grouping clause ‘by’ is dictionary which makes primary keys and other selected columns are also dictionary.
Following use of select statement generates keyed table with ‘id’ as key column:
q) t:(id:1 1 2;v:10 20 30)
q) select sum v by id from t
parsed form (note 2 dictionaries in ‘by’ clause and columns select clause)
q) ?[`t;();(enlist `id)!enlist `id;(enlist `v)!enlist (sum;`v)]
equivalent ‘exec’ will be:
q) exec v:sum v by id:id from t
Normal table is just a flipped dictionary which indirectly means grouping ‘by’ clause is false (0b) and hence select is 0b/dict. Select and Exec statements are:
q) select id,v from t
q) exec id:id,v:v by 0b from t
Parsed form (note 0b in ‘by’ clause)
q) ?[`t;();0b;`id`v!`id`v]
Hi,
Thank you so much for the detailed reply. It’s extremely helpful!
Best,
Xinyu