Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KDB: select first n rows from each group

Tags:

kdb

q-lang

How can I extract the first n rows from each group? For example: for table bb: ([]sym:(4#`a),(5#`b);val: til 9)

  sym   val
  -------------
  a     0
  a     1
  a     2
  a     3
  b     4
  b     5
  b     6
  b     7
  b     8

How can I select the first 2 rows of each group by sym?

Thanks

like image 235
ph0603 Avatar asked Mar 14 '23 10:03

ph0603


1 Answers

Can use fby:

q)select from bb where ({x in 2#x};i) fby sym
sym val
-------
a   0
a   1
b   4
b   5
like image 153
terrylynch Avatar answered Mar 24 '23 13:03

terrylynch