Search code examples
kdb

Check for a column in all tables and if column is present print the table name - kdb


We have 3 tables with below columns in our current namespace

q)t:([] a:`a`b`c; b:1 2 3);
q)s:([] a:`a`b`c; b:1 2 3);
q)z:([] z:`a`b`c; b:1 2 3);

Now we want to search and print all the tables which have column a.

Expected output: `a`s

I have two ugly solution which somewhat works

q){$[`a in cols x;x;]} each tables[];
q){(enlist x) where enlist(`a in cols x)}each tables[];

But looking for better optimal solution.


Solution

  • It'd be best to use the each right (/:) adverb with in:

    q)tables[] where `a in/: cols each tables[]
    `s`t