简体   繁体   中英

KDB Excluding rows that have nulls

I have a table that has a handful of cells with null values (scattered across the dataset). Any simple way of exluding all rows that have a null in any of its columns?

I'm just trying to avoid this...

select from T where not null col1, not null col2, not null col3, etc... 

Either use simple list functions for easy to read code or functional form for speed. The functional form will be faster as it doesn't scan all the entire columns just those that pass each stage of the filter.

q)t:flip `a`b`c`d`e!flip {5?(x;0N)} each til 10
q)t
a b c d e
---------
0       0
  1   1
  2 2 2
3 3   3 3
4 4 4 4 4
      5 5
        6
7 7
  8     8
9 9   9 9

// simple way
q)where all each not null t
,4
q)t where all each not null t
a b c d e
---------
4 4 4 4 4

// faster
q)?[t;{(not;(null;x))} each cols t; 0b; ()]
a b c d e
---------
4 4 4 4 4

Adding to Ryan's answer.....if your table has string (nested) columns then you'll need to modify the functional form a little:

q)tab:([] col1:`a`b`c`d;col2:1 0N 3 0N;col3:"v vv";col4:0n 1.0 2.0 3.0;col5:("";"";"rf";"er"))

q)tab
col1 col2 col3 col4 col5
------------------------
a    1    v         ""
b              1    ""
c    3    v    2    "rf"
d         v    3    "er"

Then using zero count as the indicator of a null string:

q)?[`tab;not,'enlist each @[(null;0=count');"C"=exec t from meta tab],'cols tab;0b;()]
col1 col2 col3 col4 col5
------------------------
c    3    v    2    "rf"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM