In SQL, you can rename columns in a query dynamically using [as “my column”]. I want to be able to pass in a column name to a function which does a select and have the resulting table use the names passed in. How can this be accomplished in q?
Thanks, –Ray
Select newname: oldname from table
Work for you?
Hi, You can achieve all this using functional select.
http://code.kx.com/wiki/JB:QforMortals2/queries_q_sql#Functional_select
Thanks Rory
there is also xcol, which allows column renaming:
f:{[cs] cs xcol select … from t }?
Thanks very much, xcol will work fine, I should have seen that myself.
q)getcol:{ c:“,” sv string (),x; value 0N!“select “,c,” from tab”} q)getcol col1col2
col1
watch out with that one - it will work only for tab as a global.
q)getcol:{ c:“,” sv string (),x; value 0N!“select “,c,” from t”}
q){t:(a:til 5);getcol `a}
“select a from t”
{c:“,” sv string (),x; value 0N!“select “,c,” from t”}
't
.:
q))
functional select is preferred for this kind of task.