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?

  • acapper

    Member
    February 1, 2024 at 12:00 am

    This can be done entirely within the update statement, using a vector conditional.

    q)t1:([] TimeStamp:2018.01.01 2018.01.01 2018.01.02 2018.01.02;stock:`AAPL`AAPL`GOOGL`GOOGL;p1: 1 2 -2 3;p2: -1 1 2 0)
    q)update r:?[(p1+p2)=0;0;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

     

    With further reading found here on vector conditionals.