代码之家  ›  专栏  ›  技术社区  ›  Carrein

是否将列名作为kdb select查询的参数传递?

kdb
  •  0
  • Carrein  · 技术社区  · 5 年前

    我想将一个列名传递到q函数中,以查询已加载的表。

    例子:

    getDistinct:{[x] select count x from raw}
    getDistinct "HEADER"
    

    这不起作用,因为Q文档说我不能将列作为参数传递。有没有办法绕过这个?

    1 回复  |  直到 5 年前
        1
  •  3
  •   DanDan4561    5 年前

    当q解释x时,它将把它当作一个字符串,它没有对列的引用,因此您的输出将只是计数“header”。

    如果要将列作为字符串传入,则需要生成整个select语句,然后使用 value

    {value "select count  ",x," from tab"} "HEADER"
    

    但是,建议的方法是使用 functional select . 下面我用 parse 要使用解析树构建函数select equivalent。

    /Create sample table
    tab:([]inst:10?`MSFT`GOOG`AAPL;time:10?.z.p;price:10?10f)
    
    /Generate my parse tree to get my functional form
    .Q.s parse "select count i by inst from tab"
    
    /Build this into my function
    {?[`tab;();(enlist x)!enlist x;(enlist `countDistinct)!enlist (#:;`i)]} `inst
    

    请注意,必须将列作为符号传入。另外,:i只是k等价于i计数。

    更新多个列

    tab:([]inst:10?`MSFT`GOOG`AAPL;time:10?.z.p;price:10?10f;cntr:10`HK`SG`UK`US)
    {?[`tab;();(x)!x;(enlist `countDistinct)!enlist (#:;`i)]} `inst`cntr
    
        2
  •  0
  •   Ferenc Bodon    5 年前

    要获取select语句的函数形式,我建议使用 buildSelect . 此外,缩小括号的范围,即使用 enlist[`countDistinct] 而不是 (enlist `countDistinct) .