Forum Replies Created

  • acapper

    Member
    February 1, 2024 at 12:00 am in reply to: How can I apply a function with ‘if’ to a table?

    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.