根据q kdb中的变量名称从表中选择列 [英] select column from a table based on a variable name in q kdb

查看:134
本文介绍了根据q kdb中的变量名称从表中选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有sym和px列的表

I have a table with columns sym and px

t:([] sym:`GOOG`IBM`APPL; px:1000 2000 3000)

现在,如果我将sym列分配给变量ab

Now, if I assign sym column to variable ab

ab:`sym

然后,在查询下面运行将给出排名错误

Then, running below query is giving rank error

select ab from t / 'rank
select `ab from t / 'rank

我有一个需求,我需要根据条件将列名保存到变量,然后在分配给变量的列上运行select查询.

I have a requirement where I need to save the column name to a variable based on a condition and then run select query on the column which is assigned to variable.

出现问死人的问题"和参考卡",但没有帮助.

Followed 'Q for Mortals' and 'Reference card' but no help.

推荐答案

有两种方法可以实现此目的.如果未对表进行键控,则只需在该列名中使用#(请注意,左侧参数必须是列表):

There are a couple of ways you could achieve this. If the table is not keyed then simply use # with that column name (note that the left hand argument must be a list):

enlist[ab]#t
sym
----
GOOG
IBM
APPL

一种替代方法是使用函数形式,例如:

An alternative is to use functional form, for example:

q)?[t;();0b;enlist[ab]!enlist ab]
sym
----
GOOG
IBM
APPL

您可以从解析树中获得此查询的形式,例如:

You can get the form of this query from the parse tree, for example:

q)parse"select ab from t"
?
`t
()
0b
(,`ab)!,`ab

以下是用于功能选择的更通用的功能:

The following is a more generic function for the functional select:

q){?[x;();0b;{x!x}(),y]}[t;ab]
sym
----
GOOG
IBM
APPL

这篇关于根据q kdb中的变量名称从表中选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆