Given a table:
q)show tbl:`date`val!/:(.z.d+til 3) cross 100+til 25;
date val
--------------
2016.10.31 100
2016.10.31 101
2016.10.31 102
2016.10.31 103
2016.10.31 104
..
Select first N rows by date: (N=2 in below example)
q)select from tbl where i in{raze y sublist/:group x}[date;2]
date val
--------------
2016.10.31 100
2016.10.31 101
2016.11.01 100
2016.11.01 101
2016.11.02 100
2016.11.02 101
Uses group
function - which will return a dictionary containing indices where each unique value occurs. The function then retrieves the first N indices (sublist) for each unique value.
NB: i
is the implicit row index in a kdb table.