KDB:如何沿着 table 的每条记录打印字符串文字?

KDB: how do I print a string literal along each record of table?

如何 select 一个 table 和一个字符串文字值?

tab:flip `items`sales`prices!(`nut`bolt`cam`cog;6 8 0 3;10 20 15 20)

select a:"abcdef", items, sales from tab

预期输出:

    a        items sales prices
    ----------------------------
    "abcdef" nut   6     10
    "abcdef" bolt  8     20
    "abcdef" cam   0     15
    "abcdef" cog   3     20

你可以这样做:

 q) update a:count[t]#enlist "abcdef" from t:select items, sales from tab

如果你有 where 子句,这也有效:

  q)update a:count[t]#enlist "abcdef" from t:select items, sales from tab where sales<4

输出:

     a      items sales prices
    ----------------------------
    "abcdef" cam   0     15
    "abcdef" cog   3     20

如果您只想向 table 添加一个新列;这里我们使用 KDB 隐藏的 index 列来计算记录。

q)update a:count[i]#enlist "abcdef" from tab
items sales prices a
---------------------------
nut   6     10     "abcdef"
bolt  8     20     "abcdef"
cam   0     15     "abcdef"
cog   3     20     "abcdef"

您可以在 select 语句中执行此操作,前提是伪造的列不是第一个

q)select items,a:count[i]#enlist"abcdef",sales from tab
items a        sales
--------------------
nut   "abcdef" 6
bolt  "abcdef" 8
cam   "abcdef" 0
cog   "abcdef" 3

如果伪造的列是第一个,那么它会将值分组到列表中,这需要 ungroup

另一种不太传统的方法是使用 cross

q)([]a:enlist "abcdef")cross tab
a        items sales prices
---------------------------
"abcdef" nut   6     10
"abcdef" bolt  8     20
"abcdef" cam   0     15
"abcdef" cog   3     20