Forum Replies Created

  • amcnaught921

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

    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/

  • amcnaught921

    Member
    January 17, 2024 at 12:00 am in reply to: Selecting date from table

    Hello,

     

    It is somewhat hard to fully answer without seeing the data, but I mocked up the following
    test:([] TRADE_DT:enlist .z.d; S_INFO_WINDCODE:`test; S_DQ_CLOSE:1.1; S_DQ_AMOUNT:2.2; S_DQ_VOLUME: 3.3)

    meta test
    c | t f a
    —————| —–
    TRADE_DT | d
    S_INFO_WINDCODE| s
    S_DQ_CLOSE | f
    S_DQ_AMOUNT | f
    S_DQ_VOLUME | f

    And from this I could do your simple select statement, there are scenario’s where column name can cause issues with select statements (e.g. if they started with underscores), in this case you can use functional select (however not required here I will post for this simple example)
    ?[`test;();0b;(enlist `TRADE_DT)!enlist `TRADE_DT]

    Both returned:

    TRADE_DT
    ———-
    2024.01.17