在已定义的函数中进行函数式选择

编程语言 2026-07-10

我有以下聚合函数:

agg:{[data; groupCol; labels] select
        req:          count i where priced=`Y,
        vol:          (sum qty where priced=`Y)%1e6,
        reqDealt:     count i where status in (`DEALT),
        volDealt:     (sum qty where status in (`DEAlT))%1e6,
        reqPcnt:      (count i where status in (`DEALT))
                       % (count i where priced=`Y),
        volPcnt:  (sum qty where status in (`DEALT))
                       % (sum qty where priced=`Y)
    by groupCol from data where label in labels};

也可以通过函数式选择表达为下方的混乱结果:

agg:{[data; groupCol; labels] ?[data;enlist (in;`label;`labels);(enlist`groupCol)!enlist`groupCol;(`req`vol`reqDealt`volDealt`reqPcnt`volPcnt)!((count;(`i;(where;(=;`priced;enlist`Y))));(%;(sum;(`qty;(where;(=;`priced;enlist`Y))));1000000f);(count;(`i;(where;(in;`status;enlist`DEALT))));(%;(sum;(`qty;(where;(in;`status;enlist`DEAlT))));1000000f);(%;(count;(`i;(where;(in;`status;enlist`DEALT))));(count;(`i;(where;(=;`priced;enlist`Y)))));(%;(sum;(`qty;(where;(in;`status;enlist`DEALT))));(sum;(`qty;(where;(=;`priced;enlist`Y))))))]};

然而,在这两种情形中,查询并未正确使用动态的BY子句,而是期望存在一个名为 groupCol 的列,其值就是在函数中传入的。它会产生一个将数据合并为单行的结果表,而不是基于我选择的 groupCol 进行逐行细分的结果。

enlist (in;`label;`labels);(enlist`groupCol)!enlist`groupCol;

我之所以把函数式选择的代码放在这里,是因为我相信对上述几行进行一些修改就能解决,但我一直得到一个 type 错误。

enlist (in;`label;labels);(groupCol)!(groupCol);

我已经尝试了 enlistlabels、以及 groupCol 的各种组合,但似乎都没能解决。非常感谢任何帮助!

解决方案

如果你想让 groupCol 动态化,这里需要使用函数式选择。

对于你发布的函数式选择的lambda,groupCol 参数实际上并未被使用。相反,你是通过名为 groupCol 的列来进行选择。

(enlist`groupCol)!enlist`groupCol

你要使用的是:

(enlist groupCol)!enlist groupCol

或者一个更简短的版本:

{x!x}enlist groupCol

你差得不远,接近的是

(groupCol)!(groupCol)

但这会抛出一个 'type,因为字典的键和值必须是一个列表。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章