# Get first row from a group

**URL:** <https://forum.kx.com/t/get-first-row-from-a-group/10718>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [January 5, 2016, 12:55am UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718 "2016-01-05T00:55:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![carfield11](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@carfield11](https://forum.kx.com/u/carfield11)\
**Post date:** [January 5, 2016, 12:55am UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/1 "2016-01-05T00:55:00Z")

</div>

Hi, if I like to select the last row from a group, I can just do

select by xxx from table

if there similar syntax to select the first row? Other than using “first” for all elements?

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [January 5, 2016, 8:38am UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/2 "2016-01-05T08:38:00Z")

</div>

Something like this?\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

select by xxx from reverse table\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

\<o:p\> \</o:p\>

Kim\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

**Von:** [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com) [[mailto:personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)] **Im Auftrag von** Carfield Yim  
**Gesendet:** Dienstag, 5. Januar 2016 01:55  
**An:** Kdb+ Personal Developers  
**Betreff:** [personal kdb+] Get first row from a group\<o:p\>\</o:p\>

\<o:p\> \</o:p\>

Hi, if I like to select the last row from a group, I can just do

select by xxx from table

if there similar syntax to select the first row? Other than using “first” for all elements?

\<o:p\>\</o:p\>

–  
Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![carfield1](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@carfield1](https://forum.kx.com/u/carfield1)\
**Post date:** [January 6, 2016, 4:15pm UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/3 "2016-01-06T16:15:00Z")

</div>

Thanks, yes, but if reverse a whole table expensive?

---

<div class="post-metadata">

**Author:** ![User7](https://avatars.discourse-cdn.com/v4/letter/u/7ea924/32.png) [@User7](https://forum.kx.com/u/User7)\
**Post date:** [January 6, 2016, 5:11pm UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/4 "2016-01-06T17:11:00Z")

</div>

Hi Carfield,

You can use this:

`xxx xkey table value exec first i by xxx from table

It’s a little more verbose, but will avoid the need to name columns and also the performance impact of a table reverse.

Regards

**Cathal Deehan**

Financial Software Developer

AQUAQ Analytics

---

<div class="post-metadata">

**Author:** ![mkrmkr1](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@mkrmkr1](https://forum.kx.com/u/mkrmkr1)\
**Post date:** [January 6, 2016, 9:43pm UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/5 "2016-01-06T21:43:00Z")

</div>

you don’t need to reverse the table. a function solution will work best. here’s an example:

q)t:(a:1 1 1 2 2 2;b:`a`b`c`d`e`f;c:10 20 30 40 50 60)

q)t

a b c

------

1 a 10

1 b 20

1 c 30

2 d 40

2 e 50

2 f 60

q)select by a from t

a| b c

-| ----

1| c 30

2| f 60

q)/is the same as

q)select last b,last c by a from t

a| b c

-| ----

1| c 30

2| f 60

q)/so for the first row

q)select first b,first c by a from t

a| b c

-| ----

1| a 10

2| d 40

q)/but for lots of columns that’s tedious so

q)?[`t;();enlist[`a]!enlist`a;(cols[`t]except`a)!(first,/:cols[`t]except`a)]

a| b c

-| ----

1| a 10

2| d 40

q)/which you could make into a little utility

q)firstrow:{[t;g]?[t;();enlist[g]!enlist g;(cols[t]except g)!(first,/:cols[t]except g)]}

q)firstrow[t;`a]

a| b c

-| ----

1| a 10

2| d 40

q)

---

<div class="post-metadata">

**Author:** ![TerryLynch1](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@TerryLynch1](https://forum.kx.com/u/TerryLynch1)\
**Post date:** [January 6, 2016, 10:04pm UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/6 "2016-01-06T22:04:00Z")

</div>

What’s wrong with

select from table where i=(first;i) fby keycol

Just have to be careful of using “i” with partitioned tables.

Also, the above can be easily changed to grab last, or first 2 rows per key etc. And can be used for multiple keys

select from table where i=(first;i) fby (keycol1;keycol2)

---

<div class="post-metadata">

**Author:** ![carfield11](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@carfield11](https://forum.kx.com/u/carfield11)\
**Post date:** [January 7, 2016, 2:56pm UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/7 "2016-01-07T14:56:00Z")

</div>

Thanks a lot everyone!

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [January 11, 2016, 12:42am UTC](https://forum.kx.com/t/get-first-row-from-a-group/10718/8 "2016-01-11T00:42:00Z")

</div>

note that this solution is the only one that works on partitioned tablescan be shortened and generalized to work with multi-column group as{?[x;();y!y,:();c!first,/:c:(cols x)except y]}(just to throw another one out there, there’s also {first each’xxgroup y}  still won’t with partitioned tables though, and it’sprobably the most expensive in both time and space)On Wed, Jan 6, 2016 at 4:43 PM, mkrmkr wrote:\> you don’t need to reverse the table. a function solution will work best.\> here’s an example:\>\> q)t:(a:1 1 1 2 2 2;b:`a`b`c`d`e`f;c:10 20 30 40 50 60)\> q)t\> a b c\> ------\> 1 a 10\> 1 b 20\> 1 c 30\> 2 d 40\> 2 e 50\> 2 f 60\> q)select by a from t\> a| b c\> -| ----\> 1| c 30\> 2| f 60\> q)/is the same as\> q)select last b,last c by a from t\> a| b c\> -| ----\> 1| c 30\> 2| f 60\> q)/so for the first row\> q)select first b,first c by a from t\> a| b c\> -| ----\> 1| a 10\> 2| d 40\> q)/but for lots of columns that’s tedious so\> q)?[`t;();enlist[`a]!enlist`a;(cols[`t]except`a)!(first,/:cols[`t]except`a)]&gt; a| b c&gt; -| ----&gt; 1| a 10&gt; 2| d 40&gt; q)/which you could make into a little utility&gt; q)firstrow:{[t;g]?[t;();enlist[g]!enlist g;(cols[t]except&gt; g)!(first,/:cols[t]except g)]}&gt; q)firstrow[t;`a]\> a| b c\> -| ----\> 1| a 10\> 2| d 40\> q)\>\>\>\>\> On Wednesday, January 6, 2016 at 11:15:43 AM UTC-5, Carfield Yim wrote:\>\>\>\> Thanks, yes, but if reverse a whole table expensive?\>\>\>\> On Tue, Jan 5, 2016 at 4:38 PM, Kim Tang \<kuen…\> wrote:\>\>\>\>\>\> Something like this?\>\>\>\>\>\>\>\>\>\>\>\> select by xxx from reverse table\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\> Kim\>\>\>\>\>\>\>\>\>\>\>\> Von: [personal...@googlegroups.com](mailto:personal...@googlegroups.com) [[mailto:personal...@googlegroups.com](mailto:personal...@googlegroups.com)]\>\>\> Im Auftrag von Carfield Yim\>\>\> Gesendet: Dienstag, 5. Januar 2016 01:55\>\>\> An: Kdb+ Personal Developers\>\>\> Betreff: [personal kdb+] Get first row from a group\>\>\>\>\>\>\>\>\>\>\>\> Hi, if I like to select the last row from a group, I can just do\>\>\>\>\>\> select by xxx from table\>\>\>\>\>\> if there similar syntax to select the first row? Other than using “first”\>\>\> for all elements?\>\>\>\>\>\> –\>\>\>

Submitted via Google Groups\</kuen…\>
