How to assign an auto-increment id in a group?

Hi, say I have a trade table with trade of multiple symbols, I would like to assign an auto-increment id per trades of individual symbols so that it is easier to compare with another trade table, but I am not sure how to do that, would you help to suggest?

There’s no built in way. There’s the i column in each table but it is dependent on order, deletions etc.  You just have to do it yourself e.g. 

q)ID:0                                                                                                                                                                             

q)tab:(a:(); id:())                                                                                                                                                              

q)insertid:{[t;x] insert[t;update id:ID+til count x from x]; ID+::count x}                                                                                                         

q)insertid[`tab;(a:8 9 10)]                                                                                                                                                      

q)tab                                                                                                                                                                              

a  id


8  0 

9  1 

10 2 

q)insertid[`tab;(a:8 9 10)]                                                                                                                                                      

q)tab                                                                                                                                                                              

a  id


8  0 

9  1 

10 2 

8  3 

9  4 

10 5 

q)delete from `tab where a=9                                                                                                                                                       

`tab

q)insertid[`tab;(a:8 9 10)]                                                                                                                                                      

q)tab                                                                                                                                                                              

a  id


8  0 

10 2 

8  3 

10 5 

8  6 

9  7 

10 8 

Thanks Jonny, I am not really understand how to apply the above logic to create auto-increment id, the trade table schema is something like this:

trade:(time:time$();sym:symbol$();price:float$();size:int$())

Say there are few rows for AAPL, and few rows for GOOG, how can I assign sequence numbers as auto-increment id to difference trades for difference symbols?

On Tue, Apr 26, 2016 at 9:24 PM, Jonny Press <pressjonny0@gmail.com> wrote:

There’s no built in way. There’s the i column in each table but it is dependent on order, deletions etc.  You just have to do it yourself e.g. 

q)ID:0                                                                                                                                                                             

q)tab:(a:(); id:())                                                                                                                                                              

q)insertid:{[t;x] insert[t;update id:ID+til count x from x]; ID+::count x}                                                                                                         

q)insertid[`tab;(a:8 9 10)]                                                                                                                                                      

q)tab                                                                                                                                                                              

a  id


8  0 

9  1 

10 2 

q)insertid[`tab;(a:8 9 10)]                                                                                                                                                      

q)tab                                                                                                                                                                              

a  id


8  0 

9  1 

10 2 

8  3 

9  4 

10 5 

q)delete from `tab where a=9                                                                                                                                                       

`tab

q)insertid[`tab;(a:8 9 10)]                                                                                                                                                      

q)tab                                                                                                                                                                              

a  id


8  0 

10 2 

8  3 

10 5 

8  6 

9  7 

10 8 

On 26 Apr 2016, at 12:03, Carfield Yim <carfield@gmail.com> wrote:

Hi, say I have a trade table with trade of multiple symbols, I would like to assign an auto-increment id per trades of individual symbols so that it is easier to compare with another trade table, but I am not sure how to do that, would you help to suggest?


Submitted via Google Groups

If you just want to treat it as a temporary ID then just use i e.g. 

update id:i from `trade

If you want the id to reset per sym (I’m not sure if that is what you are ask) then do it by sym e.g. 

update id:til count i by sym from `trade

I see, thanks a lot