KX Community

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

Home Forums KX Academy simplifying code using multiple fby possible?

Tagged: 

  • simplifying code using multiple fby possible?

    Posted by EDA_Kim on July 31, 2024 at 4:14 pm

    I want to simplify the code below into single line using two fbys but it doesn’t working.

    Is there any possible way to do it? or should I just keep using separate queries?

    tip_stat : select avg tip by payment_type from jan09 where fare > (avg;fare) fby vendor

    select payment_type, tip from tip_stat where tip = max tip

    rocuinneagain replied 3 months, 3 weeks ago 2 Members · 1 Reply
  • 1 Reply
  • rocuinneagain

    Member
    August 1, 2024 at 9:26 am
    t:([] payment_type:10#CARDCASH;vendor:10#abcd`e;fare:10?50;tip:10?50)
    t
    payment_type vendor fare tip
    ----------------------------
    CARD a 30 39
    CASH b 17 0
    CARD c 23 49
    CASH d 44 46
    CARD e 12 41
    CASH a 2 18
    CARD b 36 49
    CASH c 37 40
    CARD d 44 24
    CASH e 28 13
    select from t where ({exec (tip=max tip) and (fare>avg fare) from x};([] tip;fare)) fby vendor
    payment_type vendor fare tip
    ----------------------------
    CARD a 30 39
    CARD b 36 49

    You can pass in sub tables to aggregate on more than one column

    fby – Reference – kdb+ and q documentation – kdb+ and q documentation (kx.com)

Log in to reply.