In this link, there is an example on how to include a dynamic parameter. d
, in a KDB select query:
h: hopen`:myhost01:8012 // open connection
d: 2016.02.15 // define date var
symList: `GBPUSD`EURUSD
h raze "select from MarketDepth where date=", string d, ", sym in `GBPUSD`EURUSD" // run query with parameter d
Here d
is of type date
and is easy to string concatenate in order to generate a dynamic query.
If I want to add symList
as a dynamic parameter as well by converting to string:
raze "select from MarketDepth where date=", string d, ", sym in ", string symList
The concatenated string becomes: select from MarketDepth where date=2016.02.15, sym in GBPUSDEURUSD
, in other words the string concatenation loses the backticks so the query does not run. How can I solve this?
p.S: I know about functional querying but after failing for 2 hours, I have given up on that.
You are right,
string SYMBOL
does not preserve a backtick character, so you'll have to append it yourself like this:I used join
,
with each-both adverb'
to join a backtick with each element of a list. Having your symbol list stringified your dynamic query becomesYou can also use
parse
to see how a shape of a functional form of your query will look like.Now it's easy to create a functional select:
Hope this helps.
Update: @Ryan Hamilton's solution is probably the best in your particular scenario. You can even make a table name an argument if you want:
But it is worth noting that you can't use this technique when you need to make a list of columns dynamic. The following will NOT work:
You will have to either build a dynamic select expression like you do or use functional forms.