Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

kdb/Q: How to select rows where at least one column is not null?

I have a table that looks like this:

ID Name 1_Day_Time 2_Day_Time
2B May 762
8D Air 142
W2 Tim 908 332
9H Bee
5E Ice 712

How do I only select those rows with at least one non-null in the 1_Day_Time and 2_Day_Time columns in kdb/Q?

My intended output is below, with the row of ID 9H removed as there are nulls in both 1_Day_Time and 2_Day_Time columns:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ID Name 1_Day_Time 2_Day_Time
2B May 762
8D Air 142
W2 Tim 908 332
5E Ice 712

>Solution :

i’d advise that you not use column names starting with a number (e.g. prefer "One_Day_Time" over "1_Day_Time"), otherwise you’ll need functional select. But here are a few possible solutions:

q)show t: ([] ID:`2B`8D`W2`9H`5E; Name:`May`Air`Tim`Bee`Ice; One_Day_Time: 762 0N 908 0N 712; Two_Day_Time: 0N 143 332 0N 0N)
ID Name One_Day_Time Two_Day_Time
---------------------------------
2B May  762
8D Air               143
W2 Tim  908          332
9H Bee
5E Ice  712

q)select from t where not null[One_Day_Time] & null[Two_Day_Time]
ID Name One_Day_Time Two_Day_Time
---------------------------------
2B May  762
8D Air               143
W2 Tim  908          332
5E Ice  712

q)select from t where (not null One_Day_Time) | (not null Two_Day_Time)
ID Name One_Day_Time Two_Day_Time
---------------------------------
2B May  762
8D Air               143
W2 Tim  908          332
5E Ice  712

q)select from t where {not all null x} each (One_Day_Time,'Two_Day_Time)
ID Name One_Day_Time Two_Day_Time
---------------------------------
2B May  762
8D Air               143
W2 Tim  908          332
5E Ice  712
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading