Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KDB selecting first row from each group

Tags:

select

kdb

Very silly question... Consider the table t1 below which is sorted by sym.

t1:([]sym:(3#`A),(2#`B),(4#`C);val:10 40 12 50 58 75 22 103 108)

sym val
A   10
A   40
A   12
B   50
B   58
C   75
C   22
C   103
C   108

I want to select the first row corresponding to each sym, like this:

(`sym`val)!(`A`B`C;10j, 50j, 75j)

sym val
A   10
B   50
C   75

There's got to be a one-liner to do this. To get the LAST row for each sym, it would be as simple as select by sym from t1. Any hints?

like image 447
bigO6377 Avatar asked Dec 25 '22 14:12

bigO6377


1 Answers

select first val by sym from t1

Or for multiple columns, you can reverse the table and run your query:

select by sym from reverse t1
like image 62
jgleeson Avatar answered Dec 28 '22 10:12

jgleeson