Oskar MorgensternGuest
Hello,
Is there a way to programmatically specify column names as variables in a select statement to kdb?
Many Thanks.
kdb+ allows functional queries
http://www.timestored.com/kdb-guides/functional-queries-dynamic-sql
e.g.
q)t:([] a:1 2 3; b:4 5 6; c:7 8 9)
q)colNames:`a`c
q){?[t;();0b;x!x]} colNames
a c
—
1 7
2 8
3 9
q){?[t;();0b;x!x]} `b`c
b c
—
4 7
5 8
6 9
Or you can mangle strings together and use eval:
q)f:{value “select “,(“,” sv string x),” from t”}
q)t:([] q:til 9; w:til 9; e:9?9)
q)t
q w e
—–
0 0 4
1 1 2
2 2 7
3 3 0
4 4 1
5 5 2
6 6 1
7 7 8
8 8 8
q)f:{value “select “,(“,” sv string x),” from t”}
q)f[`q`e]
q e
—
0 4
1 2
2 7
3 0
4 1
5 2
6 1
7 8
8 8
q)