Search code examples
selectkdb

KDB selecting first row from each group


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?


Solution

  • 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