KX Community

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

Home Forums KX Academy qSQL questions _ where clause and exec results

  • qSQL questions _ where clause and exec results

    Posted by lestat-jin on May 21, 2024 at 2:24 pm

    Trying to combine two questions in one post as both are qSQL related, apologies if any confusions.

    1. Q statement follows right to left, is it also the case when processing multi where conditions? e.g.

    select from smalltrade2 where size=first size,sym=`GE

    It seems from the notebook Q first read size and then sym in the where clause, so where does not right to left order?

    2. below example from the notebook–why only assignment to a symbol would return a keyed table, when the sym itself is already in the table daily?

    exec first open, last close by sym from daily where sym like “*L” //not a keyed table, a dictionary

    exec first open, last close by sym:sym from daily where sym like “*L” //keyed table – also a dictionary

    Thanks,

    lestat-jin replied 7 months ago 2 Members · 2 Replies
  • 2 Replies
  • rocuinneagain

    Member
    May 21, 2024 at 3:00 pm

    1. qSQL has some differences to q.

    https://code.kx.com/q/ref/select/

    “select is a qSQL query template and varies from regular q syntax.”

    https://code.kx.com/q/basics/qsql/#where-phrase

    “Efficient Where phrases start with their most stringent tests.”

    Each where clause is it’s own expression. You can see inside [] the expression are executed left to right but within each expression the code is executed right to left.

    q)[(show 2;show 1);(show 4;show 3);(show 6;show 5)];
    1
    2
    3
    4
    5
    6

    2.

    https://code.kx.com/q/basics/funsql/#group-by-column

    “b is a column name. The result is a dictionary.”

    https://code.kx.com/q/basics/funsql/#group-by-columns

    “b is a list of column names. … returns a dictionary”

    https://code.kx.com/q/basics/funsql/#group-by-a-dictionary

    “b is a dictionary. Result is a dictionary in which the key is a table with columns as specified by b …”

    If you parse the queries you can see that sym:sym is group by dictionary

    q)parse"exec first open, last close by sym from daily where sym like \"*L\""
    ?
    `daily
    ,,(like;`sym;"*L")
    ,`sym
    `open`close!((*:;`open);(last;`close))

    vs:

    q)parse"exec first open, last close by sym:sym from daily where sym like \"*L\""
    ?
    `daily
    ,,(like;`sym;"*L")
    (,`sym)!,`sym
    `open`close!((*:;`open);(last;`close))
    • lestat-jin

      Member
      May 22, 2024 at 3:17 pm

      got it, thanks!

Log in to reply.