In one of my previous posts, I discussed functional form of select
, update
, exec
and delete
. If you remember, the general form of a functional select
is ?[t;c;b;a]
where t
is the table, c
is for constraints, b
for by
clause and a
for aggregation. Most of the time, you will see only these 4 arguments but functional select
can actually have up to 6 arguments!
The full form looks like this:
?[t;c;b;a;n;i]
where n
stands for number of rows and i
for specific rows you want to extract.
Let’s look at some examples!
t:([];time:5?.z.t;sym:5?`AAPL`MSFT`IBM;price:5?100;size:5?200)
A simple functional select
to call the entire table:
q)?[t;();0b;()] time sym price size ---------------------------- 04:54:11.685 MSFT 73 140 13:01:04.698 IBM 90 104 05:18:45.828 IBM 43 10 02:25:54.221 MSFT 90 1 11:32:19.305 AAPL 84 90
If we only want the first two rows, we set n
to 2
:
q)?[t;();0b;();2] time sym price size ---------------------------- 04:54:11.685 MSFT 73 140 13:01:04.698 IBM 90 104
To get last two rows, we set n
to -2
:
q)?[t;();0b;();-2] time sym price size ---------------------------- 02:25:54.221 MSFT 90 1 11:32:19.305 AAPL 84 90
If we only want first and third row, set n
to 0W
and then, i
to 0 2
,
q)?[t;();0b;();0W;0 2] time sym price size ---------------------------- 04:54:11.685 MSFT 73 140 05:18:45.828 IBM 43 10
To get first, third and fourth row:
q)?[t;();0b;();0W;0 2 3] time sym price size ---------------------------- 04:54:11.685 MSFT 73 140 05:18:45.828 IBM 43 10 02:25:54.221 MSFT 90 1
Not many people are aware of these two extra arguments so you can use them to impress your colleagues!
how to query hdb dynamic enough such that byClause, selectCols, whereCols are dynamically build and then query hdb?
something like below i have got so far, but still working on to improve it such that user pass args and build dynamic hdb query.
selectCols:`date`root`norm_sym`expiration_date;
byCols:`norm_sym`root;
whereCols:((“sym”;”`AAPL;`JPMC”);(valid;1b))
?[res1; ();byCols!byCols;(selectCols!selectCols)]