Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kdb function not working

Tags:

kdb+

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
like image 890
Sam Hayen Avatar asked Jan 25 '26 21:01

Sam Hayen


1 Answers

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.

like image 172
chrisaycock Avatar answered Jan 29 '26 13:01

chrisaycock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!