Sam Hayen
Sam Hayen

Reputation: 231

kdb update statement

Looked at all the documentation. Why is this not working? Loaded sp.q sample file given with KDB distribution. I am unable to figure out whats wrong with this statement.

q) \l sp.q
q)trade
date       sym time         price    size
-----------------------------------------
2007.07.23 a   04:48:52.665 73.53941 1000
2007.07.23 a   04:48:52.675 81.73358 600
2007.07.23 a   04:48:52.725 78.79526 900
2007.07.23 a   04:48:52.735 79.59502 600
2007.07.23 b   04:48:52.655 84.59765 200
2007.07.23 b   04:48:52.685 98.36199 500
2007.07.23 b   04:48:52.705 97.49875 700
2007.07.23 c   04:48:52.645 61.48308 900
2007.07.23 c   04:48:52.695 61.53192 700
2007.07.23 c   04:48:52.715 71.95405 200

q)trade:update size:300 from trade where sym=`c,price>71
'par
q)trade
date       sym time         price    size
-----------------------------------------
2007.07.23 a   04:48:52.665 73.53941 1000
2007.07.23 a   04:48:52.675 81.73358 600
2007.07.23 a   04:48:52.725 78.79526 900
2007.07.23 a   04:48:52.735 79.59502 600
2007.07.23 b   04:48:52.655 84.59765 200
2007.07.23 b   04:48:52.685 98.36199 500
2007.07.23 b   04:48:52.705 97.49875 700
2007.07.23 c   04:48:52.645 61.48308 900
2007.07.23 c   04:48:52.695 61.53192 700
2007.07.23 c   04:48:52.715 71.95405 200

Upvotes: 4

Views: 9127

Answers (2)

Aaron Davies
Aaron Davies

Reputation: 1230

trade is a partitioned table, which is why this is failing. what exactly are you trying to do? if you want to create a edited copy of the data in memory, you need something like

q)t:update size:300 from(select from trade)where sym=`c,price>71
q)t
date       sym time         price    size
-----------------------------------------
2007.07.23 a   04:48:52.665 73.53941 1000
2007.07.23 a   04:48:52.675 81.73358 600
2007.07.23 a   04:48:52.725 78.79526 900
2007.07.23 a   04:48:52.735 79.59502 600
2007.07.23 b   04:48:52.655 84.59765 200
2007.07.23 b   04:48:52.685 98.36199 500
2007.07.23 b   04:48:52.705 97.49875 700
2007.07.23 c   04:48:52.645 61.48308 900
2007.07.23 c   04:48:52.695 61.53192 700
2007.07.23 c   04:48:52.715 71.95405 300
q)

if you want to change the data on disk, that's quite hard in the general case. in this specific case, you can do it like this:

q){.Q.dd[.Q.par[`:.;x;`trade];`]set update size:300 from(select from trade where date=x)where sym=`c,price>71}2007.07.23
`:./2007.07.23/trade/
q)\l .
q)trade
date       sym time         price    size
-----------------------------------------
2007.07.23 a   04:48:52.665 73.53941 1000
2007.07.23 a   04:48:52.675 81.73358 600
2007.07.23 a   04:48:52.725 78.79526 900
2007.07.23 a   04:48:52.735 79.59502 600
2007.07.23 b   04:48:52.655 84.59765 200
2007.07.23 b   04:48:52.685 98.36199 500
2007.07.23 b   04:48:52.705 97.49875 700
2007.07.23 c   04:48:52.645 61.48308 900
2007.07.23 c   04:48:52.695 61.53192 700
2007.07.23 c   04:48:52.715 71.95405 300
q)

Upvotes: 4

Drew
Drew

Reputation: 29

The table is partitioned by date. You are required to specify a date to execute the query over, e.g.

trade:update size:300 from trade where sym=`c,price>71, date within (<d1>;<d2>)

Upvotes: 2

Related Questions