Comparison odd behaviour

Hello:

In my table (see below) I want the column OpenBuyOrdr being 1 when Trade=1 and EntryLevel>W_Open, so I used:

EU_D1:update  OpenBuyOrdr :Trade=1 and EntryLevel>W_Open from EU_D1

But it didn’t work.

Instead using this it works Ok!:

EU_D1:update T_Eq_1:Trade=1,EL_GT_WO:EntryLevel>W_Open from EU_D1

EU_D1:update OBO:T_Eq_1 and EL_GT_WO from EU_D1

Why?

Cheers

Francisco

Date | Open High Low Close Trade EntryLevel W_Open OpenBuyOrdr T_Eq_1 EL_GT_WO OBO ----------| ---------------------------------------------------------------------------------------- 2010.05.01| 1.32875 1.32875 1.32875 1.32875 1.32875 0 0 0 0 2010.05.02| 1.3289 1.33556 1.32865 1.33121 0 0 1.32875 1 0 0 0 2010.05.03| 1.33113 1.33118 1.3154 1.31955 0 0 1.32875 1 0 0 0 2010.05.04| 1.31953 1.32136 1.29615 1.29774 -1 1.31913 1.32875 0 0 0 0 2010.05.05| 1.29769 1.2996 1.27886 1.2816 0 0 1.32875 1 0 0 0 2010.05.06| 1.28163 1.28564 1.25148 1.2654 0 0 1.32875 1 0 0 0 2010.05.07| 1.2654 1.27977 1.25852 1.27524 0 0 1.32875 1 0 0 0 2010.05.08| 1.27574 1.27594 1.27574 1.27594 1 1.27614 1.27574 1 1 1 1 2010.05.09| 1.27604 1.29418 1.275 1.28696 0 0 1.27574 1 0 0 0 2010.05.10| 1.28664 1.30936 1.27221 1.2737 0 0 1.27574 1 0 0 0 2010.05.11| 1.27373 1.28026 1.2616 1.26396 -1 1.27333 1.27574 0 0 0 0 2010.05.12| 1.26398 1.27386 1.26054 1.26452 0 0 1.27574 1 0 0 0 2010.05.13| 1.26446 1.26831 1.25163 1.25319 1 1.26486 1.27574 0 1 0 0 2010.05.14| 1.25319 1.25752 1.23492 1.23578 -1 1.25279 1.27574 0 0 0 0 2010.05.15| 1.235 1.235 1.235 1.235 0 0 1.235 1 0 0 0 2010.05.16| 1.23475 1.23717 1.23338 1.23585 0 0 1.235 1 0 0 0 2010.05.17| 1.23587 1.24139 1.22339 1.23825 0 0 1.235 1 0 0 0 2010.05.18| 1.23827 1.24435 1.21439 1.21448 0 0 1.235 1 0 0 0 2010.05.19| 1.21445 1.24307 1.21438 1.23842 -1 1.21405 1.235 0 0 0 0 2010.05.20| 1.23836 1.25967 1.22954 1.25398 1 1.23876 1.235 1 1 1 1 2010.05.21| 1.25377 1.2672 1.24707 1.25664 0 0 1.235 1 0 0 0 2010.05.22| 1.25704 1.25734 1.25704 1.25734 0 0 1.25704 1 0 0 0 2010.05.23| 1.25744 1.25827 1.25052 1.25111 0 0 1.25704 1 0 0 0

Hi Francisco,

You can use the where clause in your update statement as this is evaluated first and returns a list of binary values referring to?indices?in the table where the conditions are true.

EU_D1: update OBO:1 from EU_D1 where Trade=1,EntryLevel>W_Open

If you are not renaming the table you can backtick the table name in the update statement to make it persist.

update OBO:1 from `EU_D1 where Trade=1,EntryLevel>W_Open

Thanks Rory

Hi Francisco,

On a further point,
You can make your first update work by bracketing off each of the conditions.
EU_D1:update? OpenBuyOrdr?:(Trade=1) and (EntryLevel>W_Open) from EU_D1?

What in effect you are doing is inserting a list of binary values into OpenBuyOrdr column and not integers but maybe this is what you require. Here is a quick example.

q)update t:(p=`p1) and (qty=300) ?from sp

s ?p ?qty t


s1 p1 300 1

s1 p2 200 0

s1 p3 400 0

s1 p4 200 0

s4 p5 100 0

s1 p6 100 0

s2 p1 300 1

s2 p2 400 0

s3 p2 200 0

s4 p2 200 0

s4 p4 300 0

s1 p5 400 0

q)meta update t:(p=`p1) and (qty=300) ?from sp

c ?| t f a

—| -----

s ?| s s

p ?| s p

qty| i

t ?| b?

You can read about this in the nested where section in the following link.?http://code.kx.com/wiki/JB:QforMortals2/queries_q_sql#The_select_and_exec_Templates?

However you are better using the “update from tab where cond1,cond2…” as it executes much faster.

Hope this helps

Thanks Rory

Hello,

Thanks a lot for your help!

Just to learn a bit more about q, please can you tell me where is documented this behaviour of parenthesis.

Cheers

Francisco

Hi Francisco,

Parens in q have the same effect as in other languages:

q)   (3+4)*2=14

q)   3+4*2=11

Jack 

Thanks for your help.

Ok, now I realize that my expression: 

EU_D1:update  OpenBuyOrdr :Trade=1 and EntryLevel>W_Open from EU_D1

was equivalent to:

EU_D1:update OpenBuyOrdr:Trade=(1 and EntryLevel>W_Open) from EU_D1

Cheers

To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1084)

>>> EU_D1:update OpenBuyOrdr :Trade=1 and EntryLevel>W_Open from =
EU_D1
>>
>> EU_D1:update OpenBuyOrdr :(Trade=1) and (EntryLevel>W_Open) from =
EU_D1
>
> Just to learn a bit more about q, please can you tell me where is =
documented this behaviour of parenthesis.

this is really more about operator precedence–q doesn’t have any[1]

in other languages, “and” might take priority over “=” and “>”, so =
“Trade=1 and EntryLevel>W_Open” could mean “(Trade=1) and =
(EntryLevel>W_Open)”

in q, it means “Trade=(1 and (EntryLevel>W_Open))” which (as a matter =
of logic) means “Trade=(EntryLevel>W_Open)”

[1] more or less. as long as there aren’t any (square) brackets or =
adverbs involved…=

Yes, I thought I was aware about the lack of operators precedence in q: 

I was not

Cheers