kdb中列中空字符列表的复杂列表

kdb中列中空字符列表的复杂列表,kdb,Kdb,我有一个由字符列表组成的联接表 q)t:([] a:`c`d; b:("";"fill")); q)s:([] a:`b`c`c; b:("";"";"")) q)select from t lj select b by a from s Output: a b --------- c ("";"") / This is the culprit want to replace it with null character d "fill" join的输出由一系列空字符组成。 我想用空字符替换它

我有一个由字符列表组成的联接表

q)t:([] a:`c`d; b:("";"fill"));
q)s:([] a:`b`c`c; b:("";"";""))
q)select from t lj select b by a from s
Output:
a b
---------
c ("";"") / This is the culprit want to replace it with null character
d "fill"
join的输出由一系列空字符组成。 我想用空字符替换它

Expected output:
     a b
    ---------
    c "" 
    d "fill"
尝试过的:几次失败的尝试

q)update b:?[null in b;raze b;b]from select from t lj select b by a from s 
q)update b:?["" in b;raze b;b]from select from t lj select b by a from s

要将空字符串列表替换为空字符串,可以尝试以下查询:

q) select from t lj  select (b;"")all""~/:b by a from s
q) select from t lj select b:raze ("";b except enlist "") by a from s
输出:

a b     
--------
c ""    
d "fill"
说明:

基本上,空字符串列表来自右表上的
group
命令。因此,在分组阶段,如果特定
a
值的分组列表(
b列值
)中的所有项都是空字符串,我们可以进行匹配。如果它们只是用一个空字符串替换它们

q) select (b;"")all""~/:b by a from s
对于
a
=c,
b
分组值为(“;”)。让我们对命令进行分解:

q) b:("";"")
q) ""~/:b  / output 11b
q) all ""~/:b  / output 1b
q)(b;"") all ""~/:b  / output ""
最后一个命令是列表索引。如果上一个命令的返回值为1b,这意味着所有项目都是空字符串,则返回
否则返回实际的b

编辑:

根据TerryLynch答案评论部分的讨论,您的要求似乎是:

  • 如果分组后
    b
    列表的所有值都是空字符串,则返回单个空字符串
  • 如果
    b
    的值混合了空字符串和非空字符串,则删除所有空字符串
为此,您可以使用以下查询:

q) select from t lj  select (b;"")all""~/:b by a from s
q) select from t lj select b:raze ("";b except enlist "") by a from s
但这将导致
b
列中不同值的不同类型。空字符串为10h,所有非空字符串列表为0h

对于一致类型,可以使用下面的查询,该查询返回
登记“”
,而不是
,但该查询不会是空字符串:

q) select from t lj select b:{(c;enlist "")()~c:x except enlist ""}b by a from s

要将空字符串列表替换为空字符串,可以尝试以下查询:

q) select from t lj  select (b;"")all""~/:b by a from s
q) select from t lj select b:raze ("";b except enlist "") by a from s
输出:

a b     
--------
c ""    
d "fill"
说明:

基本上,空字符串列表来自右表上的
group
命令。因此,在分组阶段,如果特定
a
值的分组列表(
b列值
)中的所有项都是空字符串,我们可以进行匹配。如果它们只是用一个空字符串替换它们

q) select (b;"")all""~/:b by a from s
对于
a
=c,
b
分组值为(“;”)。让我们对命令进行分解:

q) b:("";"")
q) ""~/:b  / output 11b
q) all ""~/:b  / output 1b
q)(b;"") all ""~/:b  / output ""
最后一个命令是列表索引。如果上一个命令的返回值为1b,这意味着所有项目都是空字符串,则返回
否则返回实际的b

编辑:

根据TerryLynch答案评论部分的讨论,您的要求似乎是:

  • 如果分组后
    b
    列表的所有值都是空字符串,则返回单个空字符串
  • 如果
    b
    的值混合了空字符串和非空字符串,则删除所有空字符串
为此,您可以使用以下查询:

q) select from t lj  select (b;"")all""~/:b by a from s
q) select from t lj select b:raze ("";b except enlist "") by a from s
但这将导致
b
列中不同值的不同类型。空字符串为10h,所有非空字符串列表为0h

对于一致类型,可以使用下面的查询,该查询返回
登记“”
,而不是
,但该查询不会是空字符串:

q) select from t lj select b:{(c;enlist "")()~c:x except enlist ""}b by a from s

另一种解决方案是将
的所有
b
结果一起夷为平地。使用的
子句更少,匹配(
~
)操作更少

q)update raze'/[b] from (t lj select b by a from s)
a b
--------
c ""
d "fill"
在这里,作为一种预防措施,我已经使用了更多的方法来解释未知的登记级别,然后将其应用于
lj
中的每一行。对于您的情况,更快的解决方案是

update raze each b from (t lj select b by a from s)
这将给出与Rahuls答案不同的结果

q)update raze each b from (t lj select b by a from s)
a b
--------
c "str"
d "fill"
q) select from t lj  select (b;"")all""~/:b by a from s
a b
------------
c ("";"str")
d "fill"
q)update raze each b from (t lj select b by a from s)
a b
--------
c "str"
d "fill"

另一种解决方案是将
的所有
b
结果一起夷为平地。使用的
子句更少,匹配(
~
)操作更少

q)update raze'/[b] from (t lj select b by a from s)
a b
--------
c ""
d "fill"
在这里,作为一种预防措施,我已经使用了更多的方法来解释未知的登记级别,然后将其应用于
lj
中的每一行。对于您的情况,更快的解决方案是

update raze each b from (t lj select b by a from s)
这将给出与Rahuls答案不同的结果

q)update raze each b from (t lj select b by a from s)
a b
--------
c "str"
d "fill"
q) select from t lj  select (b;"")all""~/:b by a from s
a b
------------
c ("";"str")
d "fill"
q)update raze each b from (t lj select b by a from s)
a b
--------
c "str"
d "fill"

我认为您需要决定如何处理
s
表中重复的
c
行,而不是试图修复不良结果。您正在按
a
列进行分组,但它有重复项,因此它应该如何工作。。。。它应该取第一个值,还是最后一个值?它应该将这两个字符串附加在一起吗?如果您解决了这个问题,那么您就可以避免这个问题,例如:

q)t lj select last b by a from s
a b
--------
c ""
d "fill"

我认为您需要决定如何处理
s
表中重复的
c
行,而不是试图修复不良结果。您正在按
a
列进行分组,但它有重复项,因此它应该如何工作。。。。它应该取第一个值,还是最后一个值?它应该将这两个字符串附加在一起吗?如果您解决了这个问题,那么您就可以避免这个问题,例如:

q)t lj select last b by a from s
a b
--------
c ""
d "fill"

添加了解释。添加了解释。它将清除其他可能是错误输出的
b
值。我在原始帖子中已回复添加代码块。从本质上讲,这取决于您是否希望结果中的类型一致。没错,这取决于输出。但我说的是,它会将不为null的不同值夷为可能不正确的值。试试这个表:s:([]a:`b`c`c;b:(“r”;“tt”;“yy”))它将删除其他可能是错误输出的
b
值。我在原始帖子中已回复添加代码块。从本质上讲,这取决于您是否希望结果中的类型一致。没错,这取决于输出。但我说的是,它会将不为null的不同值夷为可能不正确的值。试试这张表:([]a:`b`c`c;b:((“r”;“tt”;“yy”))有很多方法可以实现acc