Sam Hayen
Sam Hayen

Reputation: 231

kdb function not working

I am confused with this: Look at this below. Why are the lines (1) and (2) yielding different results? Thanks for your help.

    q)trade
    date       open  high  low   close volume   sym
    ------------------------------------------------
    2006.10.03 24.5  24.51 23.79 24.13 19087300 AMD
    2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
    2006.10.03 24.1  25.1  23.95 25.03 17869600 AMD
    2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
    2006.10.03 24.8  25.24 24.6  25.11 17304500 AMD
    2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
    2006.10.03 24.66 24.8  23.96 24.01 17299800 AMD
    2006.10.03 27.76 28    27.65 27.87 36452200 MSFT
    q)extr
    {[t;c;r] select from t where (`$1#'string c) within r}
    q)
    q)
    q)extr[trade;sym;`K`Z]                                     / (1)
    date       open  high  low   close volume   sym
    ------------------------------------------------
    2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
    q)
    q)
    q)select from trade where (`$1#'string sym) within `K`Z     /(2)
    date       open  high  low   close volume   sym
    ------------------------------------------------
    2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
    2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
    2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
    2006.10.03 27.76 28    27.65 27.87 36452200 MSFT

Upvotes: 3

Views: 551

Answers (1)

chrisaycock
chrisaycock

Reputation: 37930

The first one passes a variable sym to the function extr. This variable is not the column from table trade; it is a separate (global) variable.

The value of this variable sym is likely just

`AMD`MSFT

So the expression

(`$1#'string c) within r

yields the boolean array

01

rather than your desired

 01010101

That in turn means that the where portion of your select statement produces an array of indices that has value

 1

instead of

 1 3 5 7

So that one row of your table is returned, instead of the four rows you expect.

Upvotes: 3

Related Questions