KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ How can I apply a function with ‘if’ to a table? Re: How can I apply a function with ‘if’ to a table?

  • amcnaught921

    Member
    February 1, 2024 at 12:00 am

    Hey,

     

    The following will give the solution you require

    q)t1
    TimeStamp stock p1 p2
    ----------------------
    2018.01.01 AAPL 1 -1
    2018.01.01 AAPL 2 1
    2018.01.02 GOOGL -2 2
    2018.01.02 GOOGL 3 0
    
    q)f:{$[0=x+y;0;x*y]}
    
    q)t1:update r:f'[p1;p2] from t1
    
    TimeStamp stock p1 p2 r
    ------------------------
    2018.01.01 AAPL 1 -1 0
    2018.01.01 AAPL 2 1 2
    2018.01.02 GOOGL -2 2 0
    2018.01.02 GOOGL 3 0 0

    Some documentation on $ also here: https://code.kx.com/q/ref/cond/