Kdb 删除较小的副本
在KDB中,我有下表:Kdb 删除较小的副本,kdb,Kdb,在KDB中,我有下表: q)tab:flip `items`sales`prices!(`nut`bolt`cam`cog`bolt`screw;6 8 0 3 0n 0n;10 20 15 20 0n 0n) q)tab items sales prices ------------------ nut 6 10 bolt 8 20 cam 0 15 cog 3 20 bolt screw 此表中有2个重复项目(螺栓)。但是,由于第一个“螺栓
q)tab:flip `items`sales`prices!(`nut`bolt`cam`cog`bolt`screw;6 8 0 3 0n 0n;10 20 15 20 0n 0n)
q)tab
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
bolt
screw
此表中有2个重复项目(螺栓)。但是,由于第一个“螺栓”包含更多信息。我想卸下“较小”的螺栓
最终结果:
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
screw
据我所知,如果我使用'distinct'函数,它不是确定性的?因为这两行包含不同的数据,它们被认为是不同的 这取决于你如何定义“更多信息”。您可能需要提供更多的示例,但有一些可能性: 删除销售值为空的行
q)delete from tab where null sales
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
检索每个项目具有最大销售价值的行
q)select from tab where (sales*prices) = (max;sales*prices) fby items
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
一种方法是按项目向前填充,然后
bolt
将继承前面的值
q)update fills sales,fills prices by items from tab
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
bolt 8 20
screw
这也可以通过函数形式完成,您可以通过列传递表格和:
{![x;();(!). 2#enlist(),y;{x!fills,/:x}cols[x]except y]}[tab;`items]
如果“more information”表示“最少空值”,则可以计算每行中空值的数量,并仅按包含最少值的项返回这些行:
q)select from @[tab;`n;:;sum each null tab] where n=(min;n)fby items
items sales prices n
--------------------
nut 6 10 0
bolt 8 20 0
cam 0 15 0
cog 3 20 0
screw 2
虽然我不推荐这种方法,因为它需要处理行而不是列。非常感谢您的回复。我对这个问题稍加修改。与使用最大值不同,我们是否可以使用非null值,以便它可以适用于所有类型?关于fill-forward方法。如果遇到的第一个项目“螺栓”为空值,该怎么办?向前填充是否会产生将后续“螺栓”视为空值的不利影响?不,只有空值才会被填充。因此我尝试了以下命令:q)从选项卡中按项更新填充*,目的是用匹配项向前填充每个字段。它不起作用,我必须指定每个字段吗?很多TK!不幸的是,*
在q-sql中不起作用,但您可以利用。要查看查询在函数形式下的外观,可以使用parse
,例如parse“select from x”
。以下函数将允许您通过
列向前填充一个或多个:{![x;();(!).2#登记(),y;{x!填充,/:x}cols[x],但y]}
除外,其中x
是表,y
是列。我将把这个函数添加到我的答案中,因为在那里更容易阅读。