我如何从 table 行中 select 不带空值?
How do I select from a table rows with no nulls?
在 KDB 中,我有以下形式的查询:
select from t where not null a, not null b
是否可以 select 行不包含空值,而不是指定 a
和 b
?
对于一个简单的 t
那么像下面这样的东西可能会起作用:
q)t:([]a:1 2 3 4;b:1 2 0N 4;c:1 2 3 0N)
q)t
a b c
-----
1 1 1
2 2 2
3 3
4 4
q)t where not any flip null t
a b c
-----
1 1 1
2 2 2
这将检查所有列的空值,并且仅 return 那些没有空值的列的索引。对于包含字符串或列表列表的列的表,这将失败,您将需要更深入的逻辑。
@SeanHehir 提出的解决方案是正确的(我+1)。
请注意,如果 table t
被键入,它将不起作用。
要解决这个问题:
k:keys t;
xkey[k;t where not any flip null t:0!t]
这是支持字符串列(空值是 ""
)和复杂列表(假设空值始终是 ()
)的简单解决方案。
q)t:([]a:0N 1 2 4;b:("abc";"";"def";"ghi");c:(1 2 3;3 4 5;();6 7 8))
q)t
a b c
-------------
"abc" 1 2 3
1 "" 3 4 5
2 "def" ()
3 "ghi" 6 7 8
findNonNull:{
f:exec c!{$[x="C";like[;""];x in .Q.a;null;(~\:)[;()]]} each t from meta x;
x where not any f@'flip x
};
q)findNonNull t
a b c
-------------
4 "ghi" 6 7 8
首先,使用元从列名到可空函数的映射。冷 $
可以扩展以满足您的特定要求。
q)exec c!{$[x="C";like[;""];x in .Q.a;null;(~\:)[;()]]} each t from meta t
a| ^:
b| like[;""]
c| ~\:[;()]
然后将这些应用到翻转的 table 中的相应列,然后只找到非空行。
在 KDB 中,我有以下形式的查询:
select from t where not null a, not null b
是否可以 select 行不包含空值,而不是指定 a
和 b
?
对于一个简单的 t
那么像下面这样的东西可能会起作用:
q)t:([]a:1 2 3 4;b:1 2 0N 4;c:1 2 3 0N)
q)t
a b c
-----
1 1 1
2 2 2
3 3
4 4
q)t where not any flip null t
a b c
-----
1 1 1
2 2 2
这将检查所有列的空值,并且仅 return 那些没有空值的列的索引。对于包含字符串或列表列表的列的表,这将失败,您将需要更深入的逻辑。
@SeanHehir 提出的解决方案是正确的(我+1)。
请注意,如果 table t
被键入,它将不起作用。
要解决这个问题:
k:keys t;
xkey[k;t where not any flip null t:0!t]
这是支持字符串列(空值是 ""
)和复杂列表(假设空值始终是 ()
)的简单解决方案。
q)t:([]a:0N 1 2 4;b:("abc";"";"def";"ghi");c:(1 2 3;3 4 5;();6 7 8))
q)t
a b c
-------------
"abc" 1 2 3
1 "" 3 4 5
2 "def" ()
3 "ghi" 6 7 8
findNonNull:{
f:exec c!{$[x="C";like[;""];x in .Q.a;null;(~\:)[;()]]} each t from meta x;
x where not any f@'flip x
};
q)findNonNull t
a b c
-------------
4 "ghi" 6 7 8
首先,使用元从列名到可空函数的映射。冷 $
可以扩展以满足您的特定要求。
q)exec c!{$[x="C";like[;""];x in .Q.a;null;(~\:)[;()]]} each t from meta t
a| ^:
b| like[;""]
c| ~\:[;()]
然后将这些应用到翻转的 table 中的相应列,然后只找到非空行。