Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first n elements by group from table

Tags:

group-by

kdb+

Say I have the following table:

t:([]date:25#(.z.d+ til 5); travel:(5#`car),(5#`plane),(5#`bus),(5#`cycle),(5#`scooter); val:25?100)

date       travel  val
----------------------
2019.12.06 car     75 
2019.12.07 car     47 
2019.12.08 car     70 
2019.12.09 car     32 
2019.12.10 car     86 
2019.12.06 plane   29 
2019.12.07 plane   96 

How do I remove the first n observations (assuming they do not start all on 2019.12.06) by travel column?

For instance, in that particular example, if n=1, I would only get entries where date>2019.12.06.

like image 809
JejeBelfort Avatar asked Mar 03 '26 23:03

JejeBelfort


1 Answers

I would use following code snippet:

n: 3;
select from t where i>({last[y]^y@x-1}[n];i) fby travel

In above statement all row numbers are grouped by travel and first n of them are removed. {last[y]^y@x-1}[n] returns nth rownum value or last rownum, if n is larger than number of rows in group.

like image 175
Anton Dovzhenko Avatar answered Mar 06 '26 23:03

Anton Dovzhenko



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!