KDB select 正则表达式来自
KDB select regex from
有没有办法使用正则表达式来定义 select 语句中的所有列。像
select myColumnPrefix* from myTable
这将显示所有以 myColumnPrefix 开头的列?
不使用 qSQL,但是您可以使用正则表达式获取列名,然后使用函数 select。例如,
c: cols[myTable] where cols[myTable] like "myColumnPrefix*";
?[myTable;();0b;c!c]
或者作为单行,
?[myTable;();0b;{x!x@:where x like "myColumnPrefix*"} cols myTable]
如果您真的想使用 qSQL(不建议 - 很难 read/maintain),那么您可以扩展@ostewart 建议的内容:
定义table:
q)t:([]foo1:1 2;foo2:3 4;foo3:5 6;bar1:1 2;bar2:3 4;bar3:5 6)
q)t
foo1 foo2 foo3 bar1 bar2 bar3
-----------------------------
1 3 5 1 3 5
2 4 6 2 4 6
提取感兴趣的列并准备为字符串:
q)c:", " sv string cols[t] where cols[t] like "foo*";
q)c
"foo1, foo2, foo3"
将列加入 select 查询和值表达式:
q)value "select ",c," from t"
foo1 foo2 foo3
--------------
1 3 5
2 4 6
有没有办法使用正则表达式来定义 select 语句中的所有列。像
select myColumnPrefix* from myTable
这将显示所有以 myColumnPrefix 开头的列?
不使用 qSQL,但是您可以使用正则表达式获取列名,然后使用函数 select。例如,
c: cols[myTable] where cols[myTable] like "myColumnPrefix*";
?[myTable;();0b;c!c]
或者作为单行,
?[myTable;();0b;{x!x@:where x like "myColumnPrefix*"} cols myTable]
如果您真的想使用 qSQL(不建议 - 很难 read/maintain),那么您可以扩展@ostewart 建议的内容:
定义table:
q)t:([]foo1:1 2;foo2:3 4;foo3:5 6;bar1:1 2;bar2:3 4;bar3:5 6)
q)t
foo1 foo2 foo3 bar1 bar2 bar3
-----------------------------
1 3 5 1 3 5
2 4 6 2 4 6
提取感兴趣的列并准备为字符串:
q)c:", " sv string cols[t] where cols[t] like "foo*";
q)c
"foo1, foo2, foo3"
将列加入 select 查询和值表达式:
q)value "select ",c," from t"
foo1 foo2 foo3
--------------
1 3 5
2 4 6